Illegal use of mappedBy on both sides of the relationship: m
irvega Mar 25, 2007 1:37 PMI have used seam-gen's generate-entities on a simple schema I knocked up (using HSQLDB), whilst I learn the basics of SEAM, with a view to prototype a system that will run against a legacy database.
There is a many-to-many relationship between 2 tables A (the auto-generated PK is ID_) and B (the auto-generated PK is ID_) which is resolved by C which just holds the ids of A and B (as A_ID and B_ID, both columns constrained by foreign key constraints on tables A and B repectively). The schema is included below.
The generated entity A has a Set of Bs and the generated entity for B has a Set of As. All good.
When I deploy the app, using the generated ant script, I get
Illegal use of mappedBy on both sides of the relationship: m2m.B.as
(where the "as" in "B.as" is the plural of a).
There's probably something going on under the covers as I can't see anything obvious in the generated source code (see below), possibly to do with cascading deletes (?). Is this a known condition of the reverse-engineering process for many to many relationships (possibly in Hibernate rather than seam-gen)? Is there some setting that needs to be set to avoid this?
The schema looks like this
CREATE MEMORY TABLE A(ID_ BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL PRIMARY KEY) CREATE MEMORY TABLE B(ID_ BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL PRIMARY KEY) CREATE MEMORY TABLE C(A_ID BIGINT NOT NULL,B_ID BIGINT NOT NULL,CONSTRAINT C_PK PRIMARY KEY(A_ID,B_ID),CONSTRAINT C_TO_A FOREIGN KEY(A_ID) REFERENCES A(ID_),CONSTRAINT C_TO_B FOREIGN KEY(B_ID) REFERENCES B(ID_))
The generated java source code:
A.java
package m2m; // Generated 25-Mar-2007 18:15:55 by Hibernate Tools 3.2.0.b9 import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; import org.hibernate.validator.NotNull; /** * A generated by hbm2java */ @Entity @Table(name = "A", schema = "PUBLIC") public class A implements java.io.Serializable { private long id; private Set<B> bs = new HashSet<B>(0); public A() { } public A(long id) { this.id = id; } public A(long id, Set<B> bs) { this.id = id; this.bs = bs; } @Id @Column(name = "ID_", unique = true, nullable = false) @NotNull public long getId() { return this.id; } public void setId(long id) { this.id = id; } @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "as") public Set<B> getBs() { return this.bs; } public void setBs(Set<B> bs) { this.bs = bs; } }
B.java
package m2m; // Generated 25-Mar-2007 18:15:55 by Hibernate Tools 3.2.0.b9 import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; import org.hibernate.validator.NotNull; /** * B generated by hbm2java */ @Entity @Table(name = "B", schema = "PUBLIC") public class B implements java.io.Serializable { private long id; private Set<A> as = new HashSet<A>(0); public B() { } public B(long id) { this.id = id; } public B(long id, Set<A> as) { this.id = id; this.as = as; } @Id @Column(name = "ID_", unique = true, nullable = false) @NotNull public long getId() { return this.id; } public void setId(long id) { this.id = id; } @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "bs") public Set<A> getAs() { return this.as; } public void setAs(Set<A> as) { this.as = as; } }