• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Hibernate Bidirectional Mapping Problem.

Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have a parent child relationship.
Dept (One) ------ Student (Many)

Trying to persist a Dept along with its students.
Can see Dept entry in the database table.
Can see Student entries in the database table.

Problem : Student (child entity)entries have foreign key column as null in database.
What could be the problem for this?

Tried the following approaches.
1. Used <set>. Used java.util.Set in the Java class.
2. I have explicitly set parent reference to child objects.
3. Removed explicit foreign key constraint from database.

Table Details
Dept - ID(primary key) ........and other fields
Student - ID (primary key), DEPTID(foreign key), ........ and other fields.

HBM Mapping

<class entity-name="Dept" name="com.home.Dept" table="DEPT" lazy="false">
<id name="id" column="ID">
<generator class="increment"/>
<bag name="studentList" cascade="all" inverse="true" table="STUDENT" order-by="ID" lazy="false">
<key column="DEPTID" not-null="true"/>
<one-to-many entity-name="Student"/>

<class entity-name="Student" name="com.home.Student" table="STUDENT" lazy="false">
<id name="id" column="ID">
<generator class="increment"/>
<many-to-one name="department" column="DEPTID" entity-name="Dept" lazy="false" insert="false" update="false" not-null="true"/>

Class Details

public class Dept implements IDept {
private long id;
private List studentList;

public long getId() {
return id;
public void setId(long id) {
this.id = id;
public List getStudentList() {
return studentList;
public void setStudentList(List studentList) {
this.studentList = studentList;

public class Student implements IStudent {
private long id;
private Dept department;

public long getId() {
return id;
public void setId(long id) {
this.id = id;
public Dept getDepartment() {
return department;
public void setDepartment(Dept department) {
this.department = department;

Code used for persistance
IDept dept = new Department();
List studentList = new ArrayList();
Student a = new Student();
Student b = new Student();
getHibernateTemplate().save("Dept", dept); // Using Spring's Hibernate Template.

    Bookmark Topic Watch Topic
  • New Topic