-->
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.  [ 4 posts ] 
Author Message
 Post subject: Children of a one-to-many have <null> parents after sa
PostPosted: Wed Feb 07, 2007 6:55 pm 
Newbie

Joined: Wed Feb 07, 2007 6:05 pm
Posts: 10
My problem is simple, and most likely right before my eyes. But being new to Hibernate, I am unable to figure out the issue. I have 2 POJO's FilerBean and FilerShareBean. There is a one to many relationship between the FileBean and the FilerShareBean. I am attempting to use a set collection to represent this via Hibernate. Below you will see the code and mappings. The problem is when I save the FilerBean, which contains 3 FilerShareBeans in the set, to the database, the rows in the FilerShare table all have <null> FILER_ID column.

Filer Table in the database after the save.
FILER_ID: 1
NAME: alabama
DESCIPTION: Enter description here
VENDOR: IBM N-Series

FilerShare Table in the database
SHARE_ID: 1
FILER_ID: <null>
NAME: share_2
PATH: /lhome/dir_2
PROTOCOL: 0

If someone would be kind enough to take a look and let me know why this is happening, I would be greatful.

Thanks
kshenes


[b]Hibernate version:[/b] version 3.2.2, January 24, 2007

[b]Mapping documents:[/b]
<hibernate-mapping>
<class name="FilerBean" table="Filer">
<!-- the unique identifier for the Filer -->
<id name="id" column="FILER_ID">
<generator class="native"/>
</id>

<!-- name for the Filer -->
<property name="name">
<column name="NAME" sql-type="varchar(64)" not-null="true" unique="true"/>
</property>

<!-- name for the Filer -->
<property name="description">
<column name="DESCRIPTION" sql-type="varchar(128)" not-null="true"/>
</property>

<!-- name for the Filer -->
<property name="vendor">
<column name="VENDOR" sql-type="varchar(64)" not-null="true"/>
</property>

<set name="filerShares" inverse="true" cascade="all-delete-orphan">
<key column="FILER_ID"/>
<one-to-many class="FilerShareBean"/>
</set>
</class>
</hibernate-mapping>


<hibernate-mapping>
<class name="FilerShareBean" table="FilerShares">
<!-- the unique identifier for the Filer -->
<id name="id" column="SHARE_ID">
<generator class="native"/>
</id>

<many-to-one name="filer"
class="FilerBean"
column="FILER_ID"/>

<!-- name for the Share -->
<property name="name">
<column name="NAME" sql-type="varchar(64)" not-null="true"/>
</property>

<!-- path for the Share -->
<property name="path">
<column name="PATH" sql-type="varchar(128)" not-null="true"/>
</property>

<!-- protocol for the Share -->
<property name="protocol">
<column name="PROTOCOL" sql-type="integer" not-null="true"/>
</property>

</class>
</hibernate-mapping>

[b]Code between sessionFactory.openSession() and session.close():[/b]

HashSet shares = new HashSet();
for (int i = 0; i < 3; i++)
{
FilerShareBean share = new FilerShareBean("share_" + i, FilerShareBean.PROTO_CIFS, "/lhome/dir_" + i);
shares.add(share);
}
filer_.setFilerShares(shares);

Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Long filerId = (Long) session.save(filer_);
tx.commit();
session.close();


[b]Full stack trace of any exception that occurs:[/b]
No exception is thrown

[b]Name and version of the database you are using:[/b] HSQLDB 1.8.0.7

[b]The generated SQL (show_sql=true):[/b]

Hibernate:
insert
into
Filer
(FILER_ID, NAME, DESCRIPTION, VENDOR)
values
(null, ?, ?, ?)
Hibernate:
call identity()
Hibernate:
insert
into
FilerShares
(SHARE_ID, FILER_ID, NAME, PATH, PROTOCOL)
values
(null, ?, ?, ?, ?)
Hibernate:
call identity()
Hibernate:
insert
into
FilerShares
(SHARE_ID, FILER_ID, NAME, PATH, PROTOCOL)
values
(null, ?, ?, ?, ?)
Hibernate:
call identity()
Hibernate:
insert
into
FilerShares
(SHARE_ID, FILER_ID, NAME, PATH, PROTOCOL)
values
(null, ?, ?, ?, ?)
Hibernate:
call identity()
Hibernate:
select
filerbean0_.FILER_ID as FILER1_0_,
filerbean0_.NAME as NAME0_,
filerbean0_.DESCRIPTION as DESCRIPT3_0_,
filerbean0_.VENDOR as VENDOR0_
from
Filer filerbean0_
1 Filers found.
Hibernate:
select
filershare0_.FILER_ID as FILER2_1_,
filershare0_.SHARE_ID as SHARE1_1_,
filershare0_.SHARE_ID as SHARE1_1_0_,
filershare0_.FILER_ID as FILER2_1_0_,
filershare0_.NAME as NAME1_0_,
filershare0_.PATH as PATH1_0_,
filershare0_.PROTOCOL as PROTOCOL1_0_
from
FilerShares filershare0_
where
filershare0_.FILER_ID=?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 10:42 pm 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
Hi,

Before you save filer, you have to do a FilerShareBean.setFiler() for each one of them. The child needs to be told who the parent is.

The problem you are seeing is exactly what's described here.

http://www.hibernate.org/hib_docs/v3/re ... child.html

Hope this helps.

Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 12:59 pm 
Newbie

Joined: Wed Feb 07, 2007 6:05 pm
Posts: 10
After reading more closly at the example and rereading chapter 6 of
"Java Persistence with Hibernate" Mapping a parent/children relationship it became clear that the parent had to be explicitly set into the child.

Is this because the mapping for FilerShare has inverse="true" ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 11:43 pm 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
If you are saving the parent (which you are in your example), the inverse=true will avoid an additional update to the child (to set the foreign key to the parent's id). It will just be one insert for each child, with the parent id supplied during the insert. You can turn on SQL printing in hibernate config file to see this.

The fact that we need to set the parent in the child is because it is the child that maintains the link to the parent. i.e. the child has a column that contains the parent id.

Budyanto


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.