aspose file tools*
The moose likes Object Relational Mapping and the fly likes what changes should i made in save method in one-to-many relationships Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "what changes should i made in save method in one-to-many relationships " Watch "what changes should i made in save method in one-to-many relationships " New topic
Author

what changes should i made in save method in one-to-many relationships

maha laxmi
Ranch Hand

Joined: Sep 23, 2005
Posts: 68
Hi,
I've run into a problem with hibernate relationships.what is difference between bi-directional and one-to-many relationships;

in ParentObjectDaoImpl class i want to insert data in Parent table as well as child table in save() method;what changes should i made in save method;what is the meaning of lazy="true" inverse="true" and cascale="all" in mapping file;


-----------------------------------------
The classes:
------------------------


code:
--------------------------------------------------------------------------------

public class Parent implements Serializable {

/** The set of children of this object. */
private int id;
private String name;
private Set children = new HashSet();


public void setId(int id) {
this.id = id;
}

public int getId() {
return id;
}

public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}


private void setChildren(Set children) {
this.children = children;
}

private Set getChildren() {
return children;
}

public void removeChild(Child child) {
children.remove(child);
child.setParent(null);
}

public void addChild(Child child) throws IllegalArgumentException {
if (child == null) {
throw new IllegalArgumentException("Child may not be null");
}
if (child.getParent() != null) {
child.getParent().removeChild(child);
}


child.setParent(this);
children.add(child);
}


}


public class Child implements Serializable {

/** The set of children of this object. */
/** The parent. */

private int id;
private String name;

public void setId(int id) {
this.id = id;
}

public int getId() {
return id;
}

public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}


private Parent parent;

public void setParent(Parent parent) {
this.parent = parent;
}

public Parent getParent() {
return parent;
}

}

--------------------------------------------------------------------------------



code:
--------------------------------------------------------------------------------

<?xml version="1.0" ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="bus">


<class name="Parent" table="Parent">
<meta attribute="class-description">
Javadoc for the Parent class
@author
</meta>

<id name="id" column="PARENT_ID" length ="3" type="int" >
<generator class="native">
</generator>
</id>

<property name="name" type="string" column="NAME" length ="20" not-null="false"/>

<set name="children" lazy="true" inverse="true">
<key column="PARENT_ID"/>
<one-to-many class="bus.Child"/>
</set>

</class>
<class name="Child" table="Child">

<id name="id" column="id" type="string" >
<generator class="native"/>
</id>
<property name="name" type="string" column="NAME" length ="20" not-null="false"/>

<many-to-one name="parent" column="PARENT_ID" not-null="false" class="bus.Parent"/>
</class>
</hibernate-mapping>



--------------------------------------------------------------------------------

------------------------
Oracle tables:
------------------------


code:
--------------------------------------------------------------------------------

create table Parent( id varchar(50) primary key, name varchar(50));create table Child( id varchar(50) primary key, name number, parent_id varchar(50) , constraint child_parent_id foreign key (parent_id) references Parent(id));

--------------------------------------------------------------------------------

------------------------
Usage:
------------------------


code:
--------------------------------------------------------------------------------

public class ParentObjectDaoImpl extends HibernateDaoSupport implements ParentObjectDao {

public void save(Parent parent) {

try
{
HibernateTemplate ht = getHibernateTemplate();
Child child = new Child();
child.setName("xxxxxxxxxx");


parent.addChild(child);
ht.save(parent);


}catch(Exception e)
{
e.printStackTrace();
System.out.println("in catch block of ...............*********************************");
}

}
}
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

You'll have to cascade your save from parent to the child or the reverse, from child to parent.

try cascade="save-update".


If you are not laughing at yourself, then you just didn't get the joke.
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
I am not sure about your code, but the best response to your questions is RTFM. Go to hibernate.org and read the documentation, all your questions about lazy initialization, cascading, collection and inheritance mappings are there. They also have examples of how to wire these things up correctly.
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

what is the meaning of lazy="true" inverse="true" and cascale="all" in mapping file;


Sorry just forgot to answer these in my previous post.

when you are in a Hibernate Session and you load a parent object from the DB. This operation loads only the parent object and doesn't load the child objects. When you say Parent.getChildSet() or something, hibernate fetches the Children from the DB. This is called as lazily loading the objects.

As and when you you invoke some methods on the parent object which needs/uses child objects, hibernate loads it from the DB. This is specified by setting lazy="true".

If you set it to false, Hibernate loads the entire object graph when it loads the parent object.

___________________________________________________________________________

cascading option in hibernate is used to save, save and update, persist, merge the Objects. Casacding is mainly used in Object graphs say parent, child. grandchild etc.,

For eg: If you have cascade = "save-update" and you want to save the parent object.
First you create a parent object.
then you create many child objects.
you also create many grandchild objects.

Now what you typically do is create a List(grandChildren) and put the grandchilds to the child by saying child.setGrandChildren(List grandChildren).

You also set another List(children) of child objects and put it into prent Object by saying Parent.setChildren(List children).

Now in your HBM file for Parent you can have a list specification like,

<list name="children" cascade="save-update">
<...>
</list>.


Similarly in your HBM file for Child you can have a list specification like,

<list name="grandChildren" cascade="save-update">
<...>
</list>


Now saving a paren object cascades the save internally and saves both the children objects and grand children objects.

___________________________________________________________________________


Finally inverse = "true" sets the relationship between a parent and a child.
It's usulally preferred to have it on the many-side, i.e., on the child side.

setting inverse=true in the specification below,

<list name="grandChildren" cascade="save-update" inverse="true">
<...>
</list>

makes sure that the child object to which the grandChildren belon is saved first before the grandChildren are saved to Database.


Hope you are clear now.
[ March 29, 2006: Message edited by: Arun Kumarr ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: what changes should i made in save method in one-to-many relationships