Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
I am using hibernate version 3.0.5 and recently got into the weirdest of problems.
I am using relationship settings as provided by hibernate, when I use set (<set ….>) everything works just fine,
but as soon as I changed sets to lists (as per the request of team) first object in all the List objects is always null.
Database used is : Mysql version 4.0.22
Thanks in advance, any kind of suggestion will be appreciated.
-Tanya
Here is the simple example that I tried with test tables :
When I run my client program which is just printing out the children, the first object is always null.
I am sure there is something very silly that I missed out…will highly appreciate if you can help me in pointing that out.
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="mytest.model">
<class name="Parent" table="PARENT">
<id name="parentId" column="PARENT_ID" type="java.lang.Long">
<generator class="native"/>
</id>
<property name="name" column="NAME" type="java.lang.String" not-null="true" />
<list name="children" cascade="all, delete-orphan" lazy="true" inverse="true">
<index column="CHILD_ID"/>
<key column="PARENT_ID" not-null="true" update="false"/>
<one-to-many class="Child"/>
</list>
</class>
<class name="Child" table="CHILD">
<id name="childId" column="CHILD_ID" type="java.lang.Long">
<generator class="native"/>
</id>
<property name="desc" column="DESC" type="java.lang.String" />
<many-to-one name="parent" class="Parent" column="PARENT_ID"
not-null="true" insert="false" update="false"/>
</class>
</hibernate-mapping>
My test tables looks like :
PARENT CHILD
PARENT_ID (PK) CHILD_IDQ(PK)
NAME PARENT_ID
DESC
Java Code looks like this :
package mytest.model;
import java.io.Serializable;
import java.util.List;
public class Parent implements Serializable {
private int hashValue = 0;
private Long parentId;
private String name;
private List children;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getParentId() {
return parentId;
}
public void setParentId(Long parentId) {
this.parentId = parentId;
}
public List getChildren() {
return children;
}
public void setChildren(List children) {
this.children = children;
}
public boolean equals(Object rhs)
{
if (rhs == null)
return false;
if (! (rhs instanceof Parent))
return false;
Parent that = (Parent) rhs;
if (this.getParentId() != null && that.getParentId() != null)
{
if (! this.getParentId().equals(that.getParentId()))
{
return false;
}
}
return true;
}
public int hashCode()
{
if (this.hashValue == 0)
{
int parentIdValue = this.getParentId() == null ? 0 : this.getParentId().hashCode();
this.hashValue = parentIdValue;
}
return this.hashValue;
}
}
package mytest.model;
import java.io.Serializable;
public class Child implements Serializable {
private int hashValue = 0;
private Long childId;
private Long parentId;
private String desc;
private Parent parent;
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public Long getChildId()
{
return childId;
}
public void setChildId(Long childId)
{
this.hashValue = 0;
this.childId = childId;
}
public Long getParentId()
{
return this.parentId;
}
public void setParentId(Long parentId)
{
this.parentId = parentId;
}
public boolean equals(Object rhs)
{
if (rhs == null)
return false;
if (! (rhs instanceof Child))
return false;
Child that = (Child) rhs;
if (this.getChildId() != null && that.getChildId() != null)
{
if (! this.getChildId().equals(that.getChildId()))
{
return false;
}
}
return true;
}
public int hashCode()
{
if (this.hashValue == 0)
{
int childIdValue = this.getChildId() == null ? 0 : this.getChildId().hashCode();
this.hashValue = childIdValue;
}
return this.hashValue;
}
}