简体   繁体   English

无法在我的项目中创建唯一键约束

[英]Unable to create unique key constraint in my project

My table( tbl_branch_type ) exists in my database.我的表( tbl_branch_type )存在于我的数据库中。 Every other column is there, but I receive this error:每隔一列都在那里,但我收到此错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/example/local/config/LocalDbConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unable to create unique key constraint (code, bank_type_id) on table tbl_branch_type: database column 'bank_type_id' not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)

My BranchType entity is:我的BranchType实体是:

@Entity
@Table(
    name = "tbl_branch_type",
    uniqueConstraints = {
      @UniqueConstraint(
          name = "uc_branch_type_bank_id_branch_code",
          columnNames = {"code", "bank_type_id"})
    })
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Getter
@Setter
@EqualsAndHashCode(
    callSuper = true,
    exclude = {"bankType"})
@ToString(exclude = {"bankType"})
public class BranchType extends Auditing implements Serializable {

  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_branch_type")
  @SequenceGenerator(sequenceName = "seq_branch_type", allocationSize = 1, name = "seq_branch_type")
  private Long id;

  @NotNull
  @Size(min = 1, max = 20)
  @Column(name = "code", length = 20, nullable = false)
  private String code;

  @NotNull
  @Size(min = 1, max = 100)
  @Column(name = "name", length = 100, nullable = false)
  private String name;

  @JsonIgnore
  @ManyToOne
  @JsonIgnoreProperties("")
  private BankType bankType;
}

My LocalDbConfiguration class is:我的LocalDbConfiguration类是:

@Configuration
@PropertySource({"classpath:application.yml"})
@EnableJpaRepositories(
    basePackages = {"com.example.local.model.dao", "com.example.local.core.auth.repository"})
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class LocalDbConfiguration {

  @Primary
  @Bean(name = "dataSource")
  @ConfigurationProperties(prefix = "spring.datasource")
  public DataSource userDataSource() {
    return DataSourceBuilder.create().build();
  }

  @Primary
  @Bean(name = "entityManagerFactory")
  public LocalContainerEntityManagerFactoryBean entityManagerFactory(
      EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
    Map<String, Object> properties = new HashMap<>();
    properties.put("hibernate.hbm2ddl.auto", "update");
    properties.put("database.platform", "org.hibernate.dialect.Oracle10gDialect");
    return builder
        .dataSource(dataSource)
        .packages(
            "com.example.local.model.entity",
            "com.example.local.model.mapper",
            "com.example.local.core.auth.domain")
        .persistenceUnit("localPU")
        .properties(properties)
        .build();
  }

  @Primary
  @Bean(name = "transactionManager")
  public PlatformTransactionManager transactionManager(
      @Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
    return new JpaTransactionManager(entityManagerFactory);
  }

  @Bean
  @Primary
  @ConfigurationProperties("spring.datasource.hikari")
  public HikariConfig defaultHikariConfig() {
    return new HikariConfig();
  }

  @Bean
  AuditorAware<Long> auditorProvider() {
    return new AuditorProviderAware();
  }
}

I believe this is the root cause of your problem: database column 'bank_type_id' not found .我相信这是您问题的根本原因: database column 'bank_type_id' not found Try to create that column尝试创建该列

I have solved my problem by adding this codes for entityManagerFactory in LocalDbConfiguration.class我通过在 LocalDbConfiguration.class 中为 entityManagerFactory 添加此代码解决了我的问题

properties.put(
        "hibernate.physical_naming_strategy",
        "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");
    properties.put(
        "hibernate.implicit_naming_strategy",
        "org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM