I have problem persisting parent - child (School-Students) object through. I am using EJB3 JPA, Hibernate EnityManager. I am not using annotation but trying
to do with separate mapping file.
Below code only inserts school record but students donot get inserted. And, ther is not error on the console.
userTransaction.begin();
em.persist(school); // **should persist student as well**
userTransaction.commit();
Below is my object model and logic. I have doubt on mapping on school.xm and student.xml where I ahve defined association.
SCHOOL Table
===============
school_id PK
school_name
School.java
-----------------
private int school_id;
private String school_name;
private List total_students_list
Followed by setter and getter
STUDENTS Table
=================
student_id pk
student_name
school_student_FK : reference school_id of School table
Students.java
-------------------
private int student_id;
private String student_name;
private int school_student_FK;
Followed by setter and getter
school.orm.xml
---------------
<entity class="School" name="School">
<table name="SCHOOL"/>
<attributes>
// manual entered id
<id name="school_id">
<column name="school_id"/>
</id>
<basic name="school_name">
<column name="school_name" />
</basic>
<one-to-many name="total_students_list" fetch="EAGER" mapped-by="school" target-entity="Students">
<cascade><cascade-all/></cascade>
</one-to-many>
</attributes>
</entity>
<entity class="Students" name="Students">
<table name="Students"/>
<attributes>
// manual entered id
<id name="student_id">
<column name="student_id"/>
</id>
<basic name="student_name">
<column name="student_name" />
</basic>
<many-to-one name="school" target-entity="School" >
<join-column name="school_student_FK" referenced-column-name="school_id"/>
</many-to-one>
</attributes>
</entity>
Client code written in EJB
----------------------------
@TransactionManagement(TransactionManagementType.BEAN)
public class MyEJB
{
@PersistenceContext(unitName = "school")
EntityManager em;
@Resource
public UserTransaction userTransaction;
School school = new School();
school.setchool_id(100);
school.setschoolName("My School");
Student student= new Student();
student.setStudent_Id(10);
student.setStudentName("MyName");
Student student1= new Student();
student.setStudent_Id(20);
student.setStudentName("MyName");
List<Students> studentsList = new ArrayList<Students>();
studentsList.add(student);
studentsList.add(student1);
school.total_students_list(studentsList);
userTransaction.begin();
em.persist(school); // **should persist student as well**
userTransaction.commit();
}
[Message sent by forum member 'charusama' (charusama)]
http://forums.java.net/jive/thread.jspa?messageID=301782