views:

2432

answers:

3

Hello ! I'm trying to map a ManyToMany relationships between 2 tables, both having composite primary keys

LSFOCTB which primary key is composed of : LSFOC_CODSOC,LSFOC_CODLSC,LSFOC_CODFOC

LSFORTB which primary key is composed of : LSFOR_CODSOC,LSFOR_CODLSC,LSFOC_CODFOR

The table in charge of the ManyToMany relationship is :

LSFCFTB, with : LSFCF_CODSOC,LSFCF_CODLSC,LSFCF_CODFOC,LSFCF_CODFOR

So, in the hibernate model mapping LSFOCTB, I tried :

@ManyToMany(targetEntity = package.LSFOCTB.class, cascade = { CascadeType.PERSIST,
      CascadeType.MERGE })
    @JoinTable(name = "LSFCFTB", joinColumns = {
      @JoinColumn(name = "LSFCF_CODLSC", referencedColumnName = "LSFOC_CODLSC"),
      @JoinColumn(name = "LSFCF_CODFOC", referencedColumnName = "LSFOC_CODFOC"),
      @JoinColumn(name = "LSFCF_CODSOC", referencedColumnName = "LSFOC_CODSOC") }, 
    inverseJoinColumns = { @JoinColumn(name = "LSFCF_CODLSC", referencedColumnName = "LSFOR_CODLSC"),
      @JoinColumn(name = "LSFCF_CODFOR", referencedColumnName = "LSFOR_CODFOR"),
      @JoinColumn(name = "LSFCF_CODSOC", referencedColumnName = "LSFOR_CODSOC") })

before the getter. But it won't work... The error, when trying to access the distant collection is :

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [beans-dao.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for collection: package.LSFOCTB.distantCollection column: LSFCF_CODLSC

Have already managed to make an hibernate mapping work for a ManyToMany relationship ? If so, what is wrong with my mapping ? Thank you for your help !

+1  A: 

The problem seems to be that you are creating a join table with 6 columns and there are duplicate names for your columns. You are actually creating 2 columns with the name LSFCF_CODLSC and 2 columns named LSFCF_CODFOR and 2 columns named LSFCF_CODSOC.

I would suggest that you try this:

@JoinTable(name = "LSFCFTB", joinColumns = {
                    @JoinColumn(name = "LSFOC_LSFCF_CODLSC", referencedColumnName = "LSFOC_CODLSC"),
                    @JoinColumn(name = "LSFOC_LSFCF_CODFOC", referencedColumnName = "LSFOC_CODFOC"),
                    @JoinColumn(name = "LSFOC_LSFCF_CODSOC", referencedColumnName = "LSFOC_CODSOC") }, 
        inverseJoinColumns = { @JoinColumn(name = "LSFOR_LSFCF_CODLSC", referencedColumnName = "LSFOR_CODLSC"),
                    @JoinColumn(name = "LSFOR_LSFCF_CODFOR", referencedColumnName = "LSFOR_CODFOR"),
                    @JoinColumn(name = "LSFOR_LSFCF_CODSOC", referencedColumnName = "LSFOR_CODSOC") })

or something similar (according to your naming convention) to give each column a unique name.

Vincent Ramdhanie
What are the names of the columns in the table named LSFCFTB in the database? name="existing column name" and referencedColumnName="name of column being referenced"
Vincent Ramdhanie
A: 

Hello Vincent ! The thing is, I can't change the names of the column... Is there another way, without changing anything in the database (legacy schema..)? Thanks for your answer !

Anthony43
I made this comment above but: What are the names of the columns in the table named LSFCFTB in the database? name="existing column name" and referencedColumnName="name of column being referenced"
Vincent Ramdhanie
A: 

Can you resolve this issue? I have a situation to similar.

Pablo