Hibernate JPA如何将list-index hbm属性转换为注释?

时间:2014-10-14 12:31:50

标签: hibernate jpa jpa-2.0 spring-data-jpa hbm

我想通过注释将现有的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>

我的问题是属性list-index,我试图将index元素用于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

1 个答案:

答案 0 :(得分:0)

我认为你需要@OrderColumn

@OrderColumn(name="TYPE_COMPARUTION_COURRIER_TYPES_COURRIERS_IDX")
@ManyToMany(cascade = {CascadeType.ALL},fetch = FetchType.EAGER)
@JoinTable(name = "TYPES_COMPARUTIONS2TYPES_COURRIERS",indexes = { 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>();