aspose file tools*
The moose likes Object Relational Mapping and the fly likes Problem in inserting a foreign key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problem in inserting a foreign key" Watch "Problem in inserting a foreign key" New topic
Author

Problem in inserting a foreign key

vidya archana
Ranch Hand

Joined: Oct 29, 2002
Posts: 36
This is the code I've written.

The objects are : 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 java.io.Serializable;
import java.util.*;



/**
* @author 128801
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Parent implements Serializable {

private Integer parentId = null;
private String name = null;
private List childList = null;

public Parent() {
//childList = new ArrayList();
}

public void setParentId(Integer i) {
this.parentId = i;
}
public Integer getParentId() {
return this.parentId;
}

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

public void setChildList(List childList) {
this.childList = childList;
}
public List getChildList() {
return childList;
}
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 java.io.Serializable;

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 implements Serializable {
private Integer childId = null;
private Integer parentId = null;
private String name = null;
private Parent parent = null;


public Child() {

}

public void setChildId(Integer i) {
this.childId = i;
}
public Integer getChildId() {
return this.childId;
}

public void setParentId(Integer i) {
this.parentId = i;
}
public Integer getParentId() {
return this.parentId;
}

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

public void setParent(Parent parent) {
//System.out.println("comes here =="+parent.getParentId());
this.parent = parent;
//this.parentId = parent.getParentId();
//System.out.println("parent id after setting = "+this.parentId);
}
public Parent getParent() {
return this.parent;
}
}


This is Parent.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 package="object">

<class name="Parent" table="vid_parent">
<id name="parentId" type="integer" column="parent_id">
<generator class="sequence">
<param name="sequence">vid_parent_seq</param>
</generator>
</id>
<list name="childList" outer-join="auto" inverse="true" cascade="save-update">
<key>
<column name="parent_id"/>
</key>
<index column="child_id"/>
<one-to-many class="Child"/>
</list>
<property name="name">
<column name="name" length="100" not-null="false"/>
</property>
</class>
</hibernate-mapping>

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

Child.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 package="object">
<class name="Child" table="vid_child">
<id name="childId" type="integer" column="child_id">
<generator class="sequence">
<param name="sequence">vid_child_seq</param>
</generator>
</id>
<many-to-one name="parentId" class="object.Parent" column="parent_id" />
<property name="name">
<column name="name" length="100" not-null="false"/>
</property>
</class>
</hibernate-mapping>

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

In the handler class I am doing :

List childList = new ArrayList();
childList = emp.getChildList();
for(int i = 0; i<childList.size(); i++) {
Child ch = (Child) childList.get(i);
System.out.println("emp.getId == "+emp.getParentId());
ch.setParent(emp);
}


session.save(emp);

.. the problem is the parent_id value is not getting inserted in the child table. All other values are saved.
Anyone pls help..
Thanks
Vidya
 
jQuery in Action, 2nd edition
 
subject: Problem in inserting a foreign key