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

时间:2020-03-11 08:21:57

标签: java spring hibernate spring-boot

我的表(tbl_branch_type)存在于我的数据库中。每隔一列,但我收到此错误:

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)

我的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;
}

我的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();
  }
}

2 个答案:

答案 0 :(得分:0)

我相信这是您的问题的根本原因:database column 'bank_type_id' not found。尝试创建该列

答案 1 :(得分:0)

我已经通过在LocalDbConfiguration.class中为EntityManagerFactory添加此代码来解决了我的问题

INTERNALLENGTH = VARIABLE
相关问题