-->
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.  [ 8 posts ] 
Author Message
 Post subject: Simple Parent - Child mapping problem... please help...
PostPosted: Mon Nov 01, 2004 11:09 am 
Newbie

Joined: Fri Oct 29, 2004 4:20 pm
Posts: 16
Location: Grand Rapids, MI
Hibernate version:
2.1.6

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.gfs.corp.component.uoc.dto.Parent" table="GFS.PARENT">
<id name="name" type="string" column="name">
<generator class="assigned">
</generator>
</id>
<set name="children" inverse="true" cascade="all-delete-orphan">
<key column="parent_name"/>
<one-to-many class="com.gfs.corp.component.uoc.dto.Child"/>
</set>
</class>
<class name="com.gfs.corp.component.uoc.dto.Child" table="GFS.CHILD">
<id name="name" type="string" column="name">
<generator class="assigned">
</generator>
</id>
<many-to-one name="parent" column="parent_name" class="com.gfs.corp.component.uoc.dto.Parent"/>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Transaction trx = session.beginTransaction();

Parent stephen = new Parent();
stephen.setName("Stephen Earl");

Child simon = new Child();
simon.setName("Simon Earl");

stephen.addChild(simon);
simon.setParent(stephen);

session.save(stephen);

trx.commit();

Full stack trace of any exception that occurs:

Name and version of the database you are using:
Oracle 9i

The generated SQL (show_sql=true):
Hibernate: insert into GFS.PARENT (name) values (?)
Hibernate: update GFS.CHILD set parent_name=? where name=?


Debug level Hibernate log excerpt:
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.Environment <clinit>
INFO: Hibernate 2.1.6
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: uoc.hbm.xml
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: com.gfs.corp.component.uoc.dto.Parent -> GFS.PARENT
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: com.gfs.corp.component.uoc.dto.Child -> GFS.CHILD
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: HibernateSessionFactory
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing one-to-many association mappings
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.Binder bindCollectionSecondPass
INFO: Mapping collection: com.gfs.corp.component.uoc.dto.Parent.children -> GFS.CHILD
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing one-to-one association property references
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing foreign key constraints
Nov 1, 2004 9:59:38 AM net.sf.hibernate.dialect.Dialect <init>
INFO: Using dialect: net.sf.hibernate.dialect.Oracle9Dialect
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Use outer join fetching: true
Nov 1, 2004 9:59:38 AM net.sf.hibernate.util.NamingHelper getInitialContext
INFO: JNDI InitialContext properties:{}
Nov 1, 2004 9:59:38 AM net.sf.hibernate.connection.DatasourceConnectionProvider configure
INFO: Using datasource: DefaultDS
Nov 1, 2004 9:59:38 AM net.sf.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Transaction strategy: net.sf.hibernate.transaction.JTATransactionFactory
Nov 1, 2004 9:59:38 AM net.sf.hibernate.util.NamingHelper getInitialContext
INFO: JNDI InitialContext properties:{}
Nov 1, 2004 9:59:38 AM net.sf.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: instantiating TransactionManagerLookup: net.sf.hibernate.transaction.WeblogicTransactionManagerLookup
Nov 1, 2004 9:59:38 AM net.sf.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: instantiated TransactionManagerLookup
Nov 1, 2004 9:59:38 AM net.sf.hibernate.util.NamingHelper getInitialContext
INFO: JNDI InitialContext properties:{}
Nov 1, 2004 9:59:38 AM net.sf.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: instantiating TransactionManagerLookup: net.sf.hibernate.transaction.WeblogicTransactionManagerLookup
Nov 1, 2004 9:59:38 AM net.sf.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: instantiated TransactionManagerLookup
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Use scrollable result sets: true
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Use JDBC3 getGeneratedKeys(): false
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: false
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: echoing all SQL to stdout
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: cache provider: net.sf.hibernate.cache.EhCacheProvider
Nov 1, 2004 9:59:38 AM net.sf.hibernate.cfg.Configuration configureCaches
INFO: instantiating and configuring caches
Nov 1, 2004 9:59:38 AM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
Nov 1, 2004 9:59:39 AM net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Factory name: HibernateSessionFactory
Nov 1, 2004 9:59:39 AM net.sf.hibernate.util.NamingHelper getInitialContext
INFO: JNDI InitialContext properties:{}
Nov 1, 2004 9:59:39 AM net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Bound factory to JNDI name: HibernateSessionFactory
Nov 1, 2004 9:59:39 AM net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
WARNING: InitialContext did not implement EventContext
Nov 1, 2004 9:59:39 AM net.sf.hibernate.util.NamingHelper getInitialContext
INFO: JNDI InitialContext properties:{}


I must be doing something fundamentally wrong here. When I attempt to save / persist a Parent object with one Child object in it children collection I only get the Parent object saved / persisted. This example is right out of the Hibernate in Action book. Can someone comment on what I might be doing wrong which would prevent the Child object(s) from being persisted?

Thanks in advance,

Steve...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 01, 2004 5:04 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Transaction trx = session.beginTransaction();

Parent stephen = new Parent();
stephen.setName("Stephen Earl");
session.save(stephen);

Child simon = new Child();
simon.setName("Simon Earl");

// what about doing simon.setParent(stephen) in stephen.addChild(simon); ? --> best practice
stephen.addChild(simon);
simon.setParent(stephen);

trx.commit();

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: No change...
PostPosted: Tue Nov 02, 2004 10:52 am 
Newbie

Joined: Fri Oct 29, 2004 4:20 pm
Posts: 16
Location: Grand Rapids, MI
I noticed that issue and made the change. Still not persisting child objects. I noticed that I'm getting update calls for my child objects, but not inserts. I also went through the faq for what to do in this situation. Still no luck persisting child objects.

Steve...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 02, 2004 10:58 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
i think the problem is, that you use a "business-primary-key" in your child-object ...

The pk is assigned by the application and i think hibernate is not able to determine if the child is unsaved or not ... and because the id is set, an update will be performed.

I would use a different pk and generator ...

gtx
curio


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 02, 2004 4:10 pm 
Newbie

Joined: Tue Sep 28, 2004 2:01 pm
Posts: 17
anthony wrote:
Transaction trx = session.beginTransaction();

Parent stephen = new Parent();
stephen.setName("Stephen Earl");
session.save(stephen);

Child simon = new Child();
simon.setName("Simon Earl");

// what about doing simon.setParent(stephen) in stephen.addChild(simon); ? --> best practice
stephen.addChild(simon);
simon.setParent(stephen);

trx.commit();


The session.save has to occur before the children can be added? In my case a new populated parent is created long before the user decides they want to persist the whole structure.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 02, 2004 4:54 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
curio is absolutely right, read the following topics
- http://forum.hibernate.org/viewtopic.php?t=933521
- http://forum.hibernate.org/viewtopic.php?t=933496

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 09, 2005 1:56 pm 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
Does this mean you cannot do this? I would think insert should handle foreign keys automatically so the app doesnt have to handle that logic. Thanks, Chris

Code:
Transaction trx = session.beginTransaction();

Parent stephen = new Parent();
stephen.setName("Stephen Earl");

Child simon = new Child();
simon.setName("Simon Earl");

stephen.addChild(simon);
simon.setParent(stephen);

//some store or insert command on stephen...

trx.commit();


anthony wrote:
Transaction trx = session.beginTransaction();

Parent stephen = new Parent();
stephen.setName("Stephen Earl");
session.save(stephen);

Child simon = new Child();
simon.setName("Simon Earl");

// what about doing simon.setParent(stephen) in stephen.addChild(simon); ? --> best practice
stephen.addChild(simon);
simon.setParent(stephen);

trx.commit();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 09, 2005 3:22 pm 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
I read somewhere that the <key element has an update attribute in Hibernate 3.0, and it appears that this will solve this, so I will try. If anyone wants to confirm, that would be great. Thanks, Chris


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