I have a situation, which may be very simple to solve for you, but I am stalled with.
A have the following two mappings:
1. The parent class Tanulo, which has a set of Tantargy instances
Code:
<hibernate-mapping>
<class name="com.minthaka.Tanulo" table="TANULO">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="native" />
</id>
<version name="version" column="VERSION"/>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<set name="jegyek" table="TANTARGY" inverse="true" lazy="true" cascade="all">
<key>
<column name="ID" />
</key>
<one-to-many class="com.minthaka.Tantargy" />
</set>
</class>
</hibernate-mapping>
2. The child class Tantargy, which has backward reference to its Tanulo:
Code:
<hibernate-mapping>
<class name="com.minthaka.Tantargy" table="TANTARGY">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="native" />
</id>
<version name="version" column="VERSION"/>
<property name="neve" type="java.lang.String">
<column name="NEVE" />
</property>
<many-to-one name="tulajdonosa" class="com.minthaka.Tanulo" fetch="join">
<column name="TULAJDONOSA" />
</many-to-one>
<property name="osztalyzat" type="int">
<column name="OSZTALYZAT" />
</property>
</class>
</hibernate-mapping>
I am implementing the insertion into the database using the following method:
Code:
public boolean createTanulo(String neve,Set<String> tantargyak){
boolean success=false;
Session ss=sf.openSession();
ss.beginTransaction();
Tanulo t=new Tanulo();
Tantargy tt=null;
t.setName(neve);
ss.save(t);
tt=new Tantargy();
tt.setNeve("Maoas");
tt.setTulajdonosa(t);
ss.save(tt);
ss.getTransaction().commit();
ss.close();
return success;
}
I have no problems with this method if I use it as is (which is not solving the addition of multiple children, just of one). But I cannot figure how would I be able to do it in one session, since the following code:
Code:
public boolean createTanulo(String neve,Set<String> tantargyak){
boolean success=false;
Session ss=sf.openSession();
ss.beginTransaction();
Tanulo t=new Tanulo();
Tantargy tt=null;
t.setName(neve);
ss.save(t);
for(String x: tantargyak)
tt=new Tantargy();
tt.setNeve(x);
tt.setTulajdonosa(t);
ss.save(tt);
}
ss.getTransaction().commit();
ss.close();
return success;
}
is throwing me error:
Cannot add or update a child row: a foreign key constraint fails (`Aureus`.`TANTARGY`, CONSTRAINT `FK_ovvs88dknnqwaf3p5xy920rj5` FOREIGN KEY (`ID`) REFERENCES `TANULO` (`ID`))I would really appreciate any solution for this problem. Thanks