-->
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.  [ 5 posts ] 
Author Message
 Post subject: many-to-one relationships with subclasses
PostPosted: Tue Mar 16, 2004 10:50 pm 
Newbie

Joined: Sun Aug 31, 2003 8:49 am
Posts: 5
I'm sure I am missing something in the documentation to solve this problem but I need a push in the right direction. At this juncture, I seem to have part of the problem licked but not all... more on the exact problem after setting the context.

I have a class that has multiple sub-classes. I want to create a class that contains references to these various sub-classses:

class Container {
PartA partA;
PartB partB;
}

class Part {
String name;
}

class PartA extends Part {
}

class PartB extends Part {
}

I have created a mapping that looks like this:

<class name="Container" table="CONTAINER">
<id name="id" column="ID" type="long">
<generator class="identity"/>
</id>

<many-to-one name="partA" column="PARTA" class="PartA" not-null="false" cascade="all"/>
<many-to-one name="partB" column="PARTB" class="PartB" not-null="false" cascade="all"/>

</class>

<class name="Part" table="PART">
<id name="id" column="ID" type="long">
<generator class="identity"/>
</id>
<discriminator column="class" type="string"/>

<property name="name" column="NAME" type="string" length="128" not-null="true"/>

...

<subclass name="PartA"/>
<subclass name="PartB"/>
</class>

The way my application needs to work, you can have a Container in existence before any parts are added. My code to add a Part to an existing Container looks like:

// while in Hibernate session:
PartA partA; // assume valid PartA
container.setPartA(partA);
hibSession.update(container);

What I am experiencing is that I am able to save the Part sub-classes successfully, but the reference from the Container to PartA or PartB is always null. It's as if the relationship does not exist to Hibernate.

What am I missing? I have not worked with subclasses in this way before.

Thanks in advance,
-Andre'


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 17, 2004 10:47 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This should work. You may havea broken setter/getter

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Retrieval works if I hand edit the database...
PostPosted: Wed Mar 17, 2004 12:50 pm 
Newbie

Joined: Sun Aug 31, 2003 8:49 am
Posts: 5
Thank you for your reply Emmanuel.

I'm glad to hear you think this should work, at least my data model is sound.

It looks like the only issue I have is that when updating the Container, the relationship between the Container and the Part subclasses is not stored. The Parts themselves are persisted correctly. If I hand edit the relationship, I can run tests successfully to retrieve a Container with all its parts.

Clearly the update of the Container is cascading correctly to save the Part, but it is not executing any SQL to update the Container row itself.

Here's the log:
2004-03-17 11:41:57,485 [main] DEBUG HibernateContainerDAO - addPartA - starting
2004-03-17 11:41:57,485 [main] DEBUG net.sf.hibernate.impl.SessionImpl - updating [Container#1]
2004-03-17 11:41:57,485 [main] DEBUG net.sf.hibernate.engine.Cascades - processing cascades for: Container
2004-03-17 11:41:57,485 [main] DEBUG net.sf.hibernate.engine.Cascades - cascading to saveOrUpdate()
2004-03-17 11:41:57,485 [main] DEBUG net.sf.hibernate.impl.SessionImpl - saveOrUpdate() unsaved instance
2004-03-17 11:41:57,485 [main] DEBUG net.sf.hibernate.impl.SessionImpl - saving [PartA#<null>]
2004-03-17 11:41:57,485 [main] DEBUG net.sf.hibernate.engine.Cascades - processing cascades for: PartA
2004-03-17 11:41:57,485 [main] DEBUG net.sf.hibernate.engine.Cascades - done processing cascades for: PartA
2004-03-17 11:41:57,485 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Inserting entity: PartA (native id)
2004-03-17 11:41:57,485 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
2004-03-17 11:41:57,485 [main] DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider - total checked-out connections: 0
2004-03-17 11:41:57,485 [main] DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider - using pooled JDBC connection, pool size: 0
2004-03-17 11:41:57,485 [main] DEBUG net.sf.hibernate.SQL - insert into PART (NAME, class) values (?, 'PartA')
2004-03-17 11:41:57,485 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
2004-03-17 11:41:57,485 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Dehydrating entity: [PartA#<null>]
2004-03-17 11:41:57,495 [main] DEBUG net.sf.hibernate.type.StringType - binding 'Test PartA' to parameter: 1
2004-03-17 11:41:57,495 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
2004-03-17 11:41:57,495 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - closing statement
2004-03-17 11:41:57,495 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
2004-03-17 11:41:57,495 [main] DEBUG net.sf.hibernate.SQL - SELECT LAST_INSERT_ID()
2004-03-17 11:41:57,495 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
2004-03-17 11:41:57,495 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Natively generated identity: 1
2004-03-17 11:41:57,495 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
2004-03-17 11:41:57,495 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - closing statement
2004-03-17 11:41:57,495 [Finalizer] DEBUG net.sf.hibernate.impl.SessionImpl - running Session.finalize()
2004-03-17 11:41:57,495 [main] DEBUG net.sf.hibernate.engine.Cascades - processing cascades for: PartA
2004-03-17 11:41:57,495 [main] DEBUG net.sf.hibernate.engine.Cascades - done processing cascades for: PartA
2004-03-17 11:41:57,495 [main] DEBUG net.sf.hibernate.engine.Cascades - done processing cascades for: Container
2004-03-17 11:41:57,495 [main] DEBUG HibernateContainerDAO - addPart - finished

Please let me know if I can provide any more information.

Thanks again. - Andre'


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 17, 2004 1:22 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Ckeck any broken equals/hashcode method
Double check you setPartA setter
Is unsaved-value OK ?
Any broken LifeCycle interface

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Thanks... I got it
PostPosted: Wed Mar 17, 2004 6:08 pm 
Newbie

Joined: Sun Aug 31, 2003 8:49 am
Posts: 5
OK, after checking a few of these items I did identify an equals() implementation issue. That didn't fix everything, but I reviewed the doc one more time and found that I needed to use flush() in my scenario instead of update(). The Container object was already in the session...

Thanks for your quick replies. You guys are great.


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