0

I want to convert an existing hbm file to a JPA entity via annotations. the following is the hbm code i want to convert it into annotation:

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

my problem is with the attribute list-index, I tried to use indexes element into JoinTable but got exception

 @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>();

The Unit Test :

@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);
    }

I got this exception

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
Moussi
  • 470
  • 6
  • 16

1 Answers1

0

I think you need @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>();
a better oliver
  • 26,330
  • 2
  • 58
  • 66