• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ...............*********************************");
}

}
}
 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll have to cascade your save from parent to the child or the reverse, from child to parent.

try cascade="save-update".
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic