java - 无法在我的项目中创建唯一键约束
问题描述
我的表(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();
}
}
解决方案
我相信这是您问题的根本原因:database column 'bank_type_id' not found
. 尝试创建该列
推荐阅读
- google-cloud-platform - GCE 仅显示 ~15% 的使用率,但顶部显示 ~99%
- c - 如何使 SDL_ttf 与 SDL2 一起工作?
- javascript - 为什么 JS 不从 Jinja 读取数据?
- javascript - 为什么我的汉堡菜单在单击时没有打开?
- c++ - unique_ptr
构造函数要求 Deleter 不抛出 - python - 线性回归速度增加
- python - 使用 numpy 查找 True 值垂直和水平连接的索引
- java - 为什么在 c++ 库崩溃应用程序中将 java long 作为指向结构的指针传递?
- ruby-on-rails - Rails 外键分配失败
- html - 如何让按钮在手机上工作?