-->
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.  [ 13 posts ] 
Author Message
 Post subject: <joined-subclass>
PostPosted: Thu Nov 27, 2003 5:20 am 
Newbie

Joined: Thu Nov 27, 2003 4:50 am
Posts: 16
Location: M
Hallo, i'm new to Hibernate.
For a new project i evalueted some O/R mapping tools and it seems that hibernate is one of the best. The object model exists and my part is to persist it.

Inheritance is used (1 base and 4 extended classes) and i need to map it as joined-subclasses with inverse="true"

Searching the forum i found this posting:
http://forum.hibernate.org/viewtopic.php?t=925839

I was able re-produce blip's problems - but where is the mistake?
When i change the inverse to "false" i got the correct entries in the database.

Can someone send me link to a working example or can help me getting the sample running?

Cheers,
Joey


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 6:01 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I don't understand what blip is trying to suggest is wrong, so I'm amazed that you do. How about you tell us what your problem is, in terms that I will understand.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 6:28 am 
Newbie

Joined: Thu Nov 27, 2003 4:50 am
Posts: 16
Location: M
Here is my interpretation of the problem:

The 1:m ralation between the Person and the Vehicles is only stored correct in the database when the inverse setting is "false". When set to "true" the id_person is not updated in the vehicle table.

btw: I changed the "cars" one-to-many-class to test.Vehicle

These are my Testresults:

The first row is with the inverse="true"
The second row with inverse="false"

mysql> select * from vehicle;
+----------------------------------+----------------------------------+-----------+
| id_vehicle | id_person | sernum |
+----------------------------------+----------------------------------+-----------+
| 8a8a8bf8f91c5a7f00f91c5a822f0002 | NULL | 987654321 |
| 8a8a8bf8f91c5ad000f91c5ad2a20002 | 8a8a8bf8f91c5ad000f91c5ad28d0001 | 987654321 |
+----------------------------------+----------------------------------+-----------+

Could it be the bag parameter settings?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 6:35 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Show you whole mappings, please.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 7:04 am 
Newbie

Joined: Thu Nov 27, 2003 4:50 am
Posts: 16
Location: M
I hope this is what you want:

<?xml version="1.0"?>
<hibernate-mapping>
<class name="test.Person" table="person" dynamic-update="false" dynamic-insert="false">
<id name="personId" column="id_person" type="java.lang.String">
<generator class="uuid.hex"></generator>
</id>
<bag name="cars" lazy="false" inverse="true" cascade="all">
<key column="id_person"/>
<one-to-many class="test.Car"/>
</bag>
<property name="name" type="java.lang.String" update="true" insert="true" column="pname"/>
</class>
</hibernate-mapping>



<?xml version="1.0"?>
<hibernate-mapping>
<class name="test.Vehicle" table="vehicle" dynamic-update="false" dynamic-insert="false">
<id name="vehicleId" column="id_vehicle" type="java.lang.String">
<generator class="uuid.hex">
</generator>
</id>
<property name="serialNumber" type="java.lang.String" update="true" insert="true" column="sernum"/>
<many-to-one name="owner" class="test.Person" cascade="all" outer-join="false" update="true" insert="true" column="id_person"/>
<joined-subclass name="test.Car" table="cars" dynamic-update="false" dynamic-insert="false">
<key column="id_vehicle"/>
<property name="licensePlate" type="java.lang.String" update="true" insert="true" column="licplate"/>
</joined-subclass>
</class>
</hibernate-mapping>


P.S.: I don't want to prove the mapping is correct or not. I just want to see a running sample! Wouldn't it be good to put a sample into the FAQ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 7:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
And, when you add a Car to a Person, do you correctly set the backpointer by calling car.setOwner() ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 7:19 am 
Newbie

Joined: Thu Nov 27, 2003 4:50 am
Posts: 16
Location: M
Well, no. I didn't know that i have to. I saw some one-to-many samples and it was never necessary. Is this a special "subclass" bahvior?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 7:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No, this is always required for bidirectional associations. Hibernate does not implement "managed associations" (for very good reasons).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 7:36 am 
Newbie

Joined: Thu Nov 27, 2003 4:50 am
Posts: 16
Location: M
Was my misunderstanding. I thought that the saveOrUpdate in combination with the cascade="all" and inverse="true" is the managed associations mechanism.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 9:23 am 
Newbie

Joined: Thu Nov 27, 2003 4:50 am
Posts: 16
Location: M
Now i am totally confused:

in Chapter 5.4 in the Hibernate documentation i found:

Quote:
If the key column in the database is declared as NOT NULL, Hibernate may cause constraint violations. To prevent this, you must use a bidirectional association with the many-end (the set or bag) marked as inverse="true" to ensure the correct order of updates in the database.


When i use blip's sample and set the id_person to NOT NULL and use inverse="true" the sample is only working when i use the "backpointer".

I don't understand why it is working with inverse="false". Am i stupid?
In this case the documentatin should give hints when backpointers are mandatory and when not.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 9:39 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
backpointers are mandatory when you have a not-null constraint. (or any other time when inverse="true", I suppose).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 10:00 am 
Newbie

Joined: Thu Nov 27, 2003 4:50 am
Posts: 16
Location: M
Should i write a small FAQ for joined-subclasses?
It looks like that many other users have similar (understanding) problems.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 10:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This problem has nothing to do with <joined-subclass> mappings. Any other mappings behave exactly the same.


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