• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Problem in inserting a foreign key

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic