-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate and inheritance
PostPosted: Wed Mar 08, 2006 6:28 am 
Beginner
Beginner

Joined: Tue Mar 07, 2006 8:50 am
Posts: 20
Hello,

I'm just starting with Hibernate and I've got some troubles with mapping file writing.
There are three classes : Student, Book and Person.
A Student inherits from Person and has an association "HisBooks" which return Student's books collection.
This is my mapping file :

Code:
<class name="BO.Person, BO" table="Person">
   <id name="ID" type="Int32"  column="IDPerson">
      <generator class="identity"></generator>
   </id>
   <property name="FirstName" column="firstname" type="String"></property>
   <property name="LastName" column="lastname" type="String"></property>
   <property name="Age" column="age" type="Int32"></property>
   <joined-subclass name="BO.Student, BO" table="Student">
      <key column="IDPerson" foreign-key="IDStudent"></key>
      <property name="Code" column="code" type="String"></property>
      <set lazy="true" name="HisBooks">
         <key column="IDStudent" foreign-key="IDStudent"></key>
         <one-to-many class="BO.Book, BO"></one-to-many>
      </set>
   </joined-subclass>
</class>
<class name="BO.Book, BO" table="Book">
   <id name="ID" type="Int32" column="IDBook">
      <generator class="identity"></generator>
   </id>
   <property name="Name" column="name" type="String"></property>
   <property name="Code" column="code" type="Int32"></property>
</class>


As you can see, I'm using "one table per class" strategy.
Person table contents : IDPerson, firstname, lastname, age
Student table contents : IDStudent, code , IDPerson
Bool table contents : IDBool, code, IDStudent

The problem comes when I try to save a Student, because Hibernate uses the Person ID in the Book table, and not the dynamically generated Student ID (but I want to split both !)
So, how can I do, to split Person ID and Student ID, so that when I save a student, the linked books take the Student ID and not the inherited Person ID (it's clear ? lol)

Thanks


Last edited by djflex68 on Wed Mar 08, 2006 5:22 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 10:40 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:52 am
Posts: 52
Location: Zurich
Hello,
No, it is not clear to me.
Your mapping of the Student and the table Student do not match somehow.
table Student: IDStudent, code , IDPerson (your guess)
table Student: IDPerson, code (actually created by schemaExport)

Does a student really need a personID and a studentID?
Maybe it is sufficent to just have one id.
If you need a special studentID (assigned), you could create a property.

Hope this helps!
Urs


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 12:53 pm 
Beginner
Beginner

Joined: Tue Mar 07, 2006 8:50 am
Posts: 20
Thanks for this quick reply,

Yes, I think it helps.

In fact, when you say :
Quote:
Does a student really need a personID and a studentID?

That's what I wanted to do first. But, as I'm using an identity based ID (on both Student and Person tables), this means that I have to change the ID column property of the Student Table (to "not identity column", because you can not explicitly write in a identity column)
Furthermore, for someone looking at this database, it could be "curious", not to see an ID in Student which references the Person... (maybe I'm totally wrong).

In all case, I'm going to try your solution with a joined-subclass, it should work.

Thank you very much !


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 5:24 pm 
Beginner
Beginner

Joined: Tue Mar 07, 2006 8:50 am
Posts: 20
I've edited my first post (some bad translations from a French forum)


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