There are two tables parent and child
Table : Parent
Name Null? Type
----------------------------------------- -------- ---------------
ID NOT NULL NUMBER(38)
NAME VARCHAR2(100)
table:child
Name Null? Type
----------------------------------------- -------- --------------
ID NOT NULL NUMBER(38)
PARENT_ID NUMBER(38)
NAME VARCHAR2(100)
this is jst for the starting..
The code i've written is
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">
java:comp/env/jdbc/mytesting</property>
<property name="show_sql">false</property>
<property name="dialect">net.sf.hibernate.dialect.Oracle9Dialect</property>
<!-- Mapping files -->
<mapping resource="Mapping.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Mapping.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="object.Parent" table="vid_parent">
<id name="id" type="int" unsaved-value="null">
<column name="id" sql-type="int" not-null="true"/>
<generator class="sequence">
<param name="sequence">vid_parent_seq</param>
</generator>
</id>
<property name="name">
<column name="name" length="100" not-null="false"/>
</property>
</class>
<class name="object.Child" table="vid_child">
<id name="id" type="int" unsaved-value="null">
<column name="id" sql-type="int" not-null="true"/>
<generator class="sequence">
<param name="sequence">vid_child_seq</param>
</generator>
</id>
<property name="name">
<column name="name" length="100" not-null="false"/>
</property>
<many-to-one name="parentId" class="object.Parent" column="parent_id"/>
</class>
</hibernate-mapping>
Parent.java
/*
* Created on Aug 29, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package object;
import org.apache.struts.action.*;
/**
* @author 128801
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Parent extends ActionForm {
public int id;
public
String name;
public Child child;
public Parent() {
id = 0;
name = "";
}
public void setId(int i) {
this.id = i;
}
public int getId() {
return this.id;
}
public void setName(String name) {
if(name == null) name = "";
this.name = name;
}
public String getName() {
return this.name;
}
public void setChild(Child child) {
this.child = child;
}
public Child getChild() {
return child;
}
public static void main(String arg[]){
}
}
Child.java
/*
* Created on Aug 29, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package object;
import org.apache.struts.action.*;
/**
* @author 128801
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Child extends ActionForm {
private int id;
private int parentId;
private String name;
public Child() {
id = 0;
parentId = 0;
name = "";
}
public void setId(int i) {
this.id = i;
}
public int getId() {
return this.id;
}
public void setParentId(int i) {
this.parentId = i;
}
public int getParentId() {
return this.parentId;
}
public void setName(String name) {
if(name == null) name = "";
this.name = name;
}
public String getName() {
return this.name;
}
}
In createUser fuunciton()
/** adds employe info to table **/
public boolean createUser(Parent emp){
System.out.println("Creating user");
boolean status = false;
try{
this.session =sessions.openSession();
tx = session.beginTransaction();
session.save(emp);
System.out.println("identifier == "+session.getIdentifier(emp));
Child ch = new Child();
ch.setId(1);
ch.setParentId(10);
ch.setName("childname");
session.save(ch);
System.out.println("identifier1 == "+session.getIdentifier(ch));
session.flush();
tx.commit();
session.close();
status = true;
} catch (HibernateException e){
status = false;
System.out.println("eeeeeeee="+ e);
if (tx != null)
try {
tx.rollback();
} catch (HibernateException e1) {
System.out.println("rollback not successful");
}
if (session != null)
try {
session.close();
sessions.close();
} catch (HibernateException e2) {
System.out.println("session close not successful");
}
}
return status;
}
The values are getting stored for Parent. But when I included the code for storing the child values its throwing exception.
Thanks
Vidya