Thursday, March 19, 2009

Parent Child Relationship - Hibernate

Parent Child Relationship - Hibernate

Inserting values in both parent and child table in Hibernate in a single session.save() command. You need not insert value in the parent table first and then get the ID and set it in the child and then insert it. You can do both at the same time. Below are the sample code snippets of the files used.

hbm.xml file:

<class name="com.sample.pojos.UserInfoBean" table="USER_INFO" lazy="false">

... other properties configured
<property name="firstName" type="java.lang.String" column="FIRST_NAME" />
<property name="lastName" type="java.lang.String" column="LAST_NAME" />
<bag cascade="all-delete-orphan" inverse="true" lazy="true" name="userDetailsBean" table="USER_DETAILS">
<key column="USER_INFO_ID"/>
<one-to-many class="com.sample.pojos.UserDetailsBean"/>
</bag>
</class>

<class name="com.sample.pojos.UserDetailsBean" table="USER_DETAILS" lazy="false">

... other properties configured
<property name="address" type="java.lang.String" column="ADDRESS" />
<property name="city" type="java.lang.String" column="CITY" />
<many-to-one cascade="none" class="com.sample.pojos.UserInfoBean" column="USER_INFO_ID" insert="true" name="userInfoBean" outer-join="false" update="true"/>
</class>

UserInfoBean.java

private List userDetailsBean;

... other properties

/**
* @return Returns the userDetailsBean.
*/
public List getUserDetailsBean() {
return userDetailsBean;
}

/**
* @param userDetailsBean The userDetailsBean to set.
*/
public void setUserDetailsBean(List userDetailsBean) {
this.userDetailsBean = userDetailsBean;
}

... getters and setters for other properties
[/sourcecode]

UserDetailsBean.java

[sourcecode language='java']
private UserInfoBean userInfoBean;

... other properties

/**
* @return Returns the userInfoBean.
*/
public UserInfoBean getUserInfoBean() {
return userInfoBean;
}

/**
* @param userInfoBean The userInfoBean to set.
*/
public void setUserInfoBean(UserInfoBean userInfoBean) {
this.userInfoBean = userInfoBean;
}

... getters and setters for other properties

Business class:

Transaction transaction = null;

UserInfoBean userInfoBean = new UserInfoBean();

... set the required properties

UserDetailsBean userDetailsBean = new UserDetailsBean();

... set the required properties

// Setting the parent class in the child class
userDetailsBean.setUserInfoBean(userInfoBean);

// List for storing the User Details Beans
List userDetailsList = new ArrayList();

// Adding the UserDetailsBean to the list
userDetailsList.add(userDetailsBean);

// Setting the list in the UserInfoBean
userInfoBean.setUserDetailsBean(userDetailsList);

try {
Session session = SessionManager.currentSession();
transaction = session.beginTransaction();
session.save(userInfoBean);
session.flush();
session.clear();
transaction.commit();
createUserStatus = true;
} catch (Exception e) {
transaction.rollback();
} finally {
SessionManager.closeSession();
}

Now in a single session.save() command, values gets inserted in both the parent and child table.

Hope this helps.

No comments: