It's not a secret anymore!*
The moose likes Object Relational Mapping and the fly likes Need help on saving child object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Need help on saving child object " Watch "Need help on saving child object " New topic
Author

Need help on saving child object

Charu Sama
Greenhorn

Joined: Aug 03, 2006
Posts: 12
I have problem persisting parent - child (School-Students) object. 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, there is no error on the console.

userTransaction.begin();
em.persist(school);// **school is saved but should persist student as well**
userTransaction.commit();

Below is my object model and logic. I have doubt on mapping on school.xml and student.xml where I ahve defined association.


SCHOOL Table
===============
school_idPK
school_name

STUDENTS Table
=================
student_idpk
student_name
school_student_FK : reference school_id of School table


School.java
-----------------
private int school_id;
private String school_name;
private List total_students_list

Followed by setter and getter


Students.java
-------------------
private int student_id;
private String student_name;
private int school_student_FK;

Followed by setter and getter


school.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">

</one-to-many>

</attributes>
</entity>


student.xml
<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);// ** shcool is save but should save student as well**
userTransaction.commit();

}
Charu Sama
Greenhorn

Joined: Aug 03, 2006
Posts: 12
It worked. I just played with <cascade><cascade-all/></cascade> in school.xml. When I p�t cascade code I was getting this error
Caused by: java.sql.BatchUpdateException: ORA-00957: Doppelter Spaltenname

Then I removed and worked
private int school_student_FK;
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help on saving child object
 
Similar Threads
I need help with Hibernate
Bad cascade
Hibernate mapping problem
Child object is not saving.
JPA: Map composite primary key with one field being foreign key