-->
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: One-to-one mapping problem
PostPosted: Tue Mar 09, 2004 11:07 am 
Newbie

Joined: Mon Mar 08, 2004 4:10 am
Posts: 6
Hibernate version: 2.1.2
Database : DB2 v8.1

There is a one-to-one mapping between my two classes,SubFoo and Bar.
----------------------------
Mapping is:

<?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="Foo" table="foo">
<id column="ID" name="id" type="long">
<generator class="identity"></generator>
</id>
<subclass name="SubFoo" discriminator-value="subfoo">
<one-to-one name="bar" class="Bar" constrained="true" cascade="all"/>
</subclass>
</class>

<class name="Bar" table="Bar">
<id column="ID" name="id" type="long">
<generator class="foreign">
<param name="property">Bar</param>
</generator>
</class>

</hibernate-mapping>
--------------------------------
Related class :

Bar bar = new Bar();
SubFoo subfoo = new SubFoo();
subfoo.setBar(bar);
sess.save(subfoo);
---------------------------------------

While I'm persisting SubFoo bean, I take an exception as

HibernateException("unmapped property: " + propertyName); in net.sf.hibernate.persister.AbstractEntityPersister.

I looked at the example in Hibernate Users FAQ. But I couldn't solve the problem. Thanks for your interest.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 2:22 pm 
Newbie

Joined: Tue Jan 13, 2004 9:13 am
Posts: 2
Have you tried doing bar.setSubFoo(subfoo) before your .save()?


Top
 Profile  
 
 Post subject: One-to-one mapping problem
PostPosted: Fri Mar 12, 2004 6:59 am 
Newbie

Joined: Mon Mar 08, 2004 4:10 am
Posts: 6
Bar class doesn't have a reference to SubFoo class. Only SubFoo has a reference to Bar. (Unidirectional) All I want is to save Bar and SubFoo classes by cascading Bar to SubFoo. But I'm unsuccesful. I solved the problem as:

Bar bar = new Bar();
SubFoo subfoo = new SubFoo();
subfoo.setBar(bar);
sess.save(subfoo);
subfoo=sess.refresh(subfoo);
sess.save(bar,subfoo.getId());

But this is not a good solution I think. If you know about the cascading save, I'll be appreciate for your answer.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 7:03 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
have you tried

Bar bar = new Bar();
SubFoo subfoo = new SubFoo();
sess.save(subfoo);
subfoo.setBar(bar);
sess.save(bar);


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 7:05 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
oh no excuse


Bar bar = new Bar();
SubFoo subfoo = new SubFoo();
sess.save(bar );
subfoo.setBar(bar);
sess.save(subfoo);


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 22, 2004 8:41 am 
Beginner
Beginner

Joined: Mon Nov 03, 2003 11:48 pm
Posts: 29
Do you have to save the parent befor you add any children when the parent has an identity column and the children are all one-to-one with foriegn keys?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 25, 2004 8:25 am 
Newbie

Joined: Tue Jul 20, 2004 4:35 am
Posts: 7
to conqueror:

I think the problem is with the lines:
<class name="Bar" table="Bar">
<id column="ID" name="id" type="long">
<generator class="foreign">
<param name="property">Bar</param>
</generator>
</class>

There is no property named Bar in the class Bar. Just move the lines:

<generator class="foreign">
<param name="property">Bar</param>
</generator>

to the SubFoo class declaration (since this is the right class that has the property) and everything should be OK.

And there is no need to perform save two times. Your example:

Bar bar = new Bar();
SubFoo subfoo = new SubFoo();
subfoo.setBar(bar);
sess.save(subfoo);

is OK.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.