-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Null object in List for one-to-many mapping
PostPosted: Thu Aug 25, 2005 3:01 pm 
Newbie

Joined: Thu Aug 25, 2005 10:49 am
Posts: 5
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;
}
}


Top
 Profile  
 
 Post subject: null element
PostPosted: Thu Aug 25, 2005 3:06 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
the problem is here:
<index column="CHILD_ID"/>

that is supposed to be artificial, Hibernate maintained column, not the property of the child object.
Value of the column is the index of a child in the list, so if items in the list get rearranged, H takes care of it.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 4:51 pm 
Newbie

Joined: Thu Aug 25, 2005 10:49 am
Posts: 5
Thanks for your reply, but just as a clarification
>>> that is supposed to be artificial, Hibernate maintained column

May be I didn't understand it correctly, but as per my understanding I tried these options..but none worked for me ::

1. Created a getter/setter for java clas Child for index property (pls note that there is no column in the table CHILD of name INDEX.

2. Created a new column in CHILD table 'index' and getter/setter for the same , changed the index tag to <index column='INDEX'/>

Error Message for 1. Option :
[mytest.model.Parent.children#1] [select children0_.PARENT_ID as PARENT3_1_, children0_.CHILD_ID as CHILD1_1_, children0_.index as index1_, children0_.CHILD_ID as CHILD1_0_, children0_.DESC as DESC1_0_, children0_.PARENT_ID as PARENT3_1_0_ from CHILD children0_ where children0_.PARENT_ID=?]
java.sql.SQLException: Column not found message from server: "Unknown column 'children0_.index' in 'field list'"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
.........................
.........................

Results with 2. option remains the same, I still get first object as null in teh list.


Top
 Profile  
 
 Post subject: no geter/setter
PostPosted: Thu Aug 25, 2005 5:06 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
There must be NO setter/getter for index column.
Child object must NOT have index property.
Index is the position of the child in the parent's collection and H uses the column to maintain the information.

So again: table has column for index;
Mapping (hbm) tells H to use that column as index.
Child object does NOT have setters/getters - anything related to index.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 5:44 pm 
Newbie

Joined: Thu Aug 25, 2005 10:49 am
Posts: 5
Thanks for your solution. It works just fine with the changes that you suggested.
But we can't introduce one extra column in all child tables just to have list as the collection.
I know list has alot more advantages over set, but we were not looking for that.

I wish we had a better workaround then this. Again thanks for your post.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 5:53 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
If your table doesn't have an index column, and you still wish to use List as the property type, you should map the property as a Hibernate <bag>. A bag does not retain its order when it is retrieved from the database, but it may be optionally sorted or ordered.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 6:11 pm 
Newbie

Joined: Thu Aug 25, 2005 10:49 am
Posts: 5
Aaaaaaaahha you just saved my day, yep yep yep I really didn't bother
to read documentation for <bag...>, I hope Gavin won't kill me for that :)

Thanks to you too.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.