-->
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.  [ 3 posts ] 
Author Message
 Post subject: saving child object
PostPosted: Thu May 03, 2007 8:32 am 
Newbie

Joined: Wed Apr 27, 2005 5:32 am
Posts: 8
Hi
I am using hibernate 3 and Spring 2.
I have noticed when I save newly created parent object along with a list of child objects, (also newly created) with the id generator sequence (oracle)the foreign key field in the child objects are saved to the DB but not into the objects themselves:
I have class Publisher which has a collection of objects of type Author: set<Author> authors. the mapping file for Publisher:
******************************************************
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.test.domain">
<class name="Publisher" table="PUBLISHERS" schema="MYSCHEMA" lazy="false">
<id name="id" type="java.lang.Long">
<column name="PUBLISHER_ID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">PUBLISHER_ID</param>
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="100" not-null="true" />
</property>

</property>
<set name="authors" cascade="all" table="AUTHORS" lazy="false">
<key column="PUBLISHER_ID"/>
<one-to-many class="Author"/>
</set>
</class>
</hibernate-mapping>
*************************************************
mapping file for Author:
************************************************
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.test.domain">
<class name="Author" table="AUTHORS" schema="MYSCHEMA" lazy="false">

<id name="id" type="java.lang.Long">
<column name="AUTHOR_ID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">AUTHOR_ID</param>
</generator>
</id>

<property name="publisherId" type="java.lang.Long">
<column name="PUBLISHER_ID" precision="22" scale="0" />
</property>

<property name="displayName" type="java.lang.String">
<column name="DISPLAY_NAME" length="200" not-null="true" />
</property>

<property name="email" type="java.lang.String">
<column name="EMAIL" />
</property>

</class>
</hibernate-mapping>
*********************************************************
Here is the java code:
*****************************************************
Author author = new Author();
Publisher publisher = new Publisher();

try {

author.setDisplayName("Test Name");
author.setEmail("Test Email");



Set authors = new HashSet();
authors.add(author);


publisher.setAuthors(authors);
publisher.setName("Test PublisherName");


getHibernateTemplate().save(publisher);
System.out.println("Author.PublisherId = "+author.getPublisherId()); // output:"Author.PublisherId = null"


} catch (Exception e) {
System.out.println("Exception: "+e.getMessage());
e.printStackTrace();
}

**************************************************

It seems like hibernate generates id (primary keys) but fails to insert foreign keys. It also throws error if in the DB the foreign key (Author.publisherId) is set to NOT NULL, because as HQL shows, hibernate first creates two records (Publisher and Author) with two insert statements
and then updates author's publisherId.

Please tell me where I went wrong.
Many thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 03, 2007 8:55 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I think you probably want to implement a bidirectional association there. There may be a circumstance that you would want to navigate the domain graph from Author to Publisher. Instead of this in your Author mapping:
Code:
<property name="publisherId" type="java.lang.Long">
<column name="PUBLISHER_ID" precision="22" scale="0" />
</property>

Do this:
Code:
<many-to-one name="publisher" class="Publisher">
<column name="PUBLISHER_ID" precision="22" scale="0" />
</property>


In the Publisher mapping, add inverse="true" to the set of Authors.

You will, of course, have to change your mapping files, changing the Author POJO from Long publisherId to Publisher publisher.

Also, you should add bidirectional association management routines in your POJO, as such (Publisher):
Code:
  public void addAuthor(Author author) {
    if (authors == null) {
      authors = new HashSet();
    }
    authors.add(author);
    author.setPublisher(this);
  }


I leave it up to you to define an equivalent removeAuthor method.

More information on bi-directional associations here.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 09, 2007 12:35 pm 
Newbie

Joined: Wed Apr 27, 2005 5:32 am
Posts: 8
Does it mean that to make hibernate work correctly I have to make the relationship bidirectional and all unidirectional associations are doomed to work wrong?


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