【发布时间】:2014-10-14 12:31:49
【问题描述】:
我想通过注释将现有的 hbm 文件转换为 JPA 实体。 以下是我想将其转换为注释的hbm代码:
<list name="typesCourriers" table="TYPES_COMPARUTIONS2TYPES_COURRIERS" inverse="false">
<key foreign-key="TYPE_COURRIER_TYPES_COMPARUTIONS_FKC">
<column name="TYPES_COMPARUTIONS_FK" sql-type="BIGINT"/>
</key>
<list-index column="TYPE_COMPARUTION_COURRIER_TYPES_COURRIERS_IDX"/>
<many-to-many class="TypeCourrier" foreign-key="TYPE_COMPARUTION_COURRIER_TYPES_COURRIEC">
<column name="TYPES_COURRIERS_FK" sql-type="BIGINT"/>
</many-to-many>
</list>
我的问题是属性列表索引,我尝试在 JoinTable 中使用索引元素但出现异常
@ManyToMany(cascade = {CascadeType.ALL},fetch = FetchType.EAGER)
@JoinTable(name = "TYPES_COMPARUTIONS2TYPES_COURRIERS",indexes = { @Index(columnList="TYPE_COMPARUTION_COURRIER_TYPES_COURRIERS_IDX", name="TYPE_COMPARUTION_COURRIER_TYPES_COURRIERS_IDX") } ,joinColumns = {
@JoinColumn(name = "TYPES_COMPARUTIONS_FK", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "TYPES_COURRIERS_FK", nullable = false, updatable = false) })
private Set<TypeCourrier> typeCourriers= new HashSet<TypeCourrier>();
单元测试:
@Test
public void test() {
TypeComparutionCourrier typeComparutionCourrier=repository.findOne(new Long(1));
System.out.println(typeComparutionCourrier);
for(TypeCourrier typeCourrier:typeComparutionCourrier.getTypeCourriers())
{
System.out.println(typeCourrier);
}
typeComparutionCourrier.getTypeCourriers().add(typecourrierrepository.findOne(new Long(9)));
System.out.println(("*********"));
for(TypeCourrier typeCourrier:typeComparutionCourrier.getTypeCourriers())
{
System.out.println(typeCourrier);
}
repository.save(typeComparutionCourrier);
}
我遇到了这个异常
Caused by: org.hibernate.AnnotationException: Unable to create unique key constraint (TYPE_COMPARUTION_COURRIER_TYPES_COURRIERS_IDX) on table TYPES_COMPARUTIONS2TYPES_COURRIERS: database column 'TYPE_COMPARUTION_COURRIER_TYPES_COURRIERS_IDX' 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)
at org.hibernate.cfg.Configuration.buildUniqueKeyFromColumnNames(Configuration.java:1682)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1457)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
... 62 more
【问题讨论】:
-
TYPE_COMPARUTION_COURRIER_TYPES_COURRIERS_IDX 列是否存在于 TYPES_COMPARUTIONS2TYPES_COURRIERS 表中?
-
@CoverosGene 是的,它存在
标签: hibernate jpa jpa-2.0 spring-data-jpa hbm