简体   繁体   中英

Unable to create unique key constraint in my project

My table( tbl_branch_type ) exists in my database. 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:

@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:

@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 . Try to create that column

I have solved my problem by adding this codes for entityManagerFactory in LocalDbConfiguration.class

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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