Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help on saving child object

 
Charu Sama
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic