Hi -
I have some issues with inverse and bidirectional many-to-one association.
The issues arises under the simplified circumstances:
A is many-to-one
B
B has a set of
A.
Java classes are
Code:
public class A {
int id;
B b;
private void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setB(B b) {
this.b = b;
}
public B getB() {
return b;
}
}
Code:
public class B {
int id;
Set<A> as = new HashSet<A>(0);
private void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setAs(Set<A> as) {
this.as = as;
}
public Set<A> getAs() {
return as;
}
}
Mapping files are
Code:
<hibernate-mapping>
<class name="A" table='"A"' schema='"public"' lazy="true">
<id name="id" type="int">
<column name='"Id"' />
<generator class="sequence">
<param name="sequence">"A_Id_seq"</param>
</generator>
</id>
<many-to-one name="b" class="B" fetch="select">
<column name='"bId"' not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping>
<class name="B" table='"B"' schema='"public"' lazy="true">
<id name="id" type="int">
<column name='"Id"' />
<generator class="sequence">
<param name="sequence">"B_Id_seq"</param>
</generator>
</id>
<set name="as" inverse="false" lazy="true">
<key>
<column name='"bId"' not-null="true"/>
</key>
<one-to-many class="A" />
</set>
</class>
</hibernate-mapping>
The issues I am having, is when I create a new A and B, binding them via B.getAs().add(a)
Code:
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
B b = new B();
A a = new A();
Set<A> as = b.getAs();
as.add(a);
b.setAs(as);
b.save();
a.save();
session.getTransaction().commit();
I am getting
Quote:
org.hibernate.PropertyValueException: not-null property references a null or transient value: A.b
What does work is:
code]
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
B b = new B();
A a = new A();
a.setB(b);
b.save();
a.save();
session.getTransaction().commit();
[/code]
I thought inverse="false", should have taken care of this problem?
Is there any solution to this issue or am I forced to change my app server so that everything is done in the second manner?