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 ...............*********************************");
}
}
}