-->
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: Issue with joined-subclass
PostPosted: Fri Feb 17, 2006 12:11 pm 
Newbie

Joined: Fri Feb 17, 2006 9:10 am
Posts: 4
Hibernate version:
3.1
Mapping documents:
Code:
    <class name="User" table="users" schema="public">
       
        <id name="cod">
            <column name="userid" not-null="true"/>
            <generator class="sequence">
               <param name="sequence">usuarios_usuarioid_seq</param>
            </generator>
        </id>
       
        <property name="name" type="string">
            <column name="name" />
        </property>
       
        <property name="passwd" type="string">
            <column name="passwd"  />
        </property>
               
    </class>

   <joined-subclass name="Teacher" table="teacher" extends="User">
   
      <key column="userid" />
            
      <bag name="colDisciplinas" table="disciplinasprofessores" lazy="true">
         <key column="userid" />
         <many-to-many class="Discipline" column="disciplineid"/>
      </bag>
   
   </joined-subclass>



Hi there!
First, sorry if my english is bad.
Now, the problem.
I'm having an issue with a joined-subclass. I have an user table and a teacher table. A teacher is also a user, so Teacher extends User, and I'm using the table-per-subclass strategy. Also there's many-to-many mapping in the Teacher class.
It's working smootly when loading is involved, so I assume there's no critical error in the mapping.
The thing is when I try to insert a teacher.
In the system, a teacher register itself, so I insert it in the user table. The system don't know yet if this user will be a teacher.
After that, the admin of the system can change the user role, marking him as a teacher, so I insert a line in the teacher table. But when I do that, Hibernate also insert a line in the user table, assuming it's a new user, but that's not the case, since I already have an user with that id. As a result of this, I get 2 lines in the user table with the same data.
Is there a way to make Hibernate think 'look, I already have an user with that id, so I will just insert it in the teachers table'??

In any case, thanks in advance :).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 17, 2006 1:01 pm 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

I know I don't answer to your question, but you could delete the old user record from database before inserting the new teacher record in database.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 17, 2006 1:16 pm 
Regular
Regular

Joined: Wed Feb 08, 2006 3:59 pm
Posts: 75
TOMBATLECRUISEr wrote:
Hi,

I know I don't answer to your question, but you could delete the old user record from database before inserting the new teacher record in database.


Given the problem, I think this is the correct thing to do.

When you mark an user as teacher, what you're implicitly doing is changing an object's class which is bad IMO. A better way to do it would be to add a roles table. Alternatively you could use a single-table hierarchy : users and teachers in the same table with a discriminator column. Then you could update one's discriminator to make it a teacher. Still this is "class changing" : you've got to be extra carefull, with caches and detached objects for instance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 17, 2006 2:03 pm 
Newbie

Joined: Fri Feb 17, 2006 9:10 am
Posts: 4
Hi.

Deleting the old user worked, but this do not make me happy, because I believe is a very ugly way to solve the problem, since it will also delete all the records related to that user.

Quote:
When you mark an user as teacher, what you're implicitly doing is changing an object's class which is bad IMO. A better way to do it would be to add a roles table.


Actually I have a roles table (used for Tomcat security and stuff), but the thing is that a teacher has more attributes then an user, and I can't think of another solution for this, avoiding the class changing, as you say. And is impossible in this case to create a teacher right from the start, as the system doesn't know who really is a teacher until the admin says so.

I think I'll need to do the single-table hierarchy. It's not an aproach I like (the user table will be enormous), but it's the better solution right now IMO.

Anyone can give me another ideia about how to deal with this, or the single-table hierarchy is my solution?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 19, 2006 5:17 pm 
Regular
Regular

Joined: Sat Nov 19, 2005 2:46 pm
Posts: 69
Thank you for raising this question. I have a similar heirarchy where an Organization has many Person objects. Some Person objects should be Advsior objects, which have more attributes. Finally a User is an Advisor who also has a logon/password.
So my heirarchy is Person<-Advisor<-User.
I am on the edge of hitting the same problem, and I don't know if it will work but my intention was to:
Code:
Advisor fred = getFred(); // this advisor already exists
Advisor newFred = new User(fred); // this copy constructor will carry over all data such as contact phone number, email, etc
session.delete(fred); // I really need to do this?
session.update(newFred); // will this work? or I need to session.save()?


I did not come across this problem yet, because I hit a different one first:
Code:
organization.getAdvisors(); // is returning all Persons - I want only Advisors
organization.getUsers(); // is returning all Persons - I want only Users


I cannot find the right mapping to make this work... :-(

_________________
Stewart
London, UK


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 20, 2006 8:20 am 
Newbie

Joined: Fri Feb 17, 2006 9:10 am
Posts: 4
In my tests, I've loaded an user, copied his information to a teacher object, and then tried this:
1- delete the user and insert a new teacher: worked, but all the data with foreign keys refering to the user table was also deleted (and it's the right thing to do in this case, since I really deleted an user).
2 - delete the user and update the teacher: no way, Hibernate throws a NonUniqueObjectException (associate two different instances of the same java class with a particular identifier, in the scope of a single Session (this is in the javadoc)).
3 - just update the teacher: not worked also: throws a StaleObjectStateException with the message Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect).

So, I'm still in the some point where all started, lol.

And about you're loading problem, did you really mapped as a joined-sublass? Once i've had the same problem as you, and the problem was that in the java code I extended , but mapped as two separeted classes.
Look your mappings and see if helps.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 20, 2006 9:27 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Well, you've tryed to solve the inheritance problem using table-per-subclass solution. How about using table-per-hierarchy solution ? Maybe this is the right stategy for your problem.

If this new approach doesn't fit to your requests, maybe you should transform your is-a relation to a has-a relation ( teacher is a user will be user has a teacher <or not>).


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