Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.hibernate.MappingException: Repeated column in mapping for collection using @JoinTable

I have the following (simplified) situation:

Fields for TABLE A:

  • ID
  • COMMONID

Fields for TABLE AB:

  • AID
  • BID COMMONID

Fields for TABLE B:

  • ID
  • COMMONID

and want to map it with entities using a OneToMany like this (in the master class mapped on table A):

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = false, fetch = FetchType.LAZY)
@JoinTable(name = "AB", 
joinColumns= {
    @JoinColumn(name = "AID", referencedColumnName="ID"), 
    @JoinColumn(name = "COMMONID", referencedColumnName="COMMONID")},
inverseJoinColumns = {
    @JoinColumn(name = "BID", referencedColumnName="ID"), 
    @JoinColumn(name = "COMMONID", referencedColumnName="COMMONID")})
private Set<MyClassForB> list= new HashSet<MyClassForB>();

Building the session, I obtain the following error:

org.hibernate.MappingException: Repeated column in mapping for collection using @JoinTable list column: COMMONID

What am I doing wrong? Consider that I'm new to Hibernate.

like image 203
Marco Perasso Avatar asked Feb 06 '13 16:02

Marco Perasso


1 Answers

Try to add insertable, and updateable like this

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = false, fetch = FetchType.LAZY)
@JoinTable(name = "AB", 
joinColumns= {
    @JoinColumn(name = "AID", referencedColumnName="ID", insertable = false, updatable = false), 
    @JoinColumn(name = "COMMONID", referencedColumnName="COMMONID", insertable = false, updatable = false)},
inverseJoinColumns = {
    @JoinColumn(name = "BID", referencedColumnName="ID", insertable = false, updatable = false), 
    @JoinColumn(name = "COMMONID", referencedColumnName="COMMONID", insertable = false, updatable = false)})
private Set<MyClassForB> list= new HashSet<MyClassForB>();

But I'm not sure whether you will able to update this set, my use case was read only.

like image 170
Jakub Dyszkiewicz Avatar answered Oct 04 '22 11:10

Jakub Dyszkiewicz