Reputation: 71
I have the following (simplified) situation:
Fields for TABLE A:
Fields for TABLE AB:
Fields for TABLE B:
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.
Upvotes: 7
Views: 1547
Reputation: 19
Assuming you allow your JPA provider (e.g. Hibernate) create the tables...try this
Class A{
@ManyToMany
@JoinTable(
name = "AB",
joinColumns = @JoinColumn(name = "A_Id"),
inverseJoinColumns = @JoinColumn(name = "B_Id")
)
private Set<B> blist= new HashSet<B>();
}
Class B{
@ManyToMany(cascade = CascadeType.ALL, mappedBy = "A_Id")
private Set<A> blist= new HashSet<A>();
}
Upvotes: 0
Reputation: 534
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.
Upvotes: 1