-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: "table per class hierarchy" strategy question
PostPosted: Thu Jan 13, 2005 3:01 am 
Beginner
Beginner

Joined: Thu Jun 10, 2004 6:52 am
Posts: 29
Supose I have a User base class and two subclasses Admin and Customer mapped as subclasses to User with the discriminators "A" and "C". Now suposedly I want to turn a Customer into Admin (I admit, not the best example, but I am sure you understand what I mean).

How do I do that since the discriminator must be save/update = false if I remember correctly. Am I missing something here ?

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 3:32 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
I want to turn a Customer into Admin

you're in java... impossible

you have to delete and create the new typed instance

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 3:40 am 
Beginner
Beginner

Joined: Thu Jun 10, 2004 6:52 am
Posts: 29
Common, it must be a way to do that, or at least to avoid needing this. I can't just delete the Admin, what if it has a lot of foreign key constraints etc. It must be a way to solve the problem.

In a e-commerce site for example,it must be a way to turn an Order into a DeliveredOrder w/o actually deleting and recreating it (It has OrderItems attached etc. you mean I have to delete them too, create the DeliveredOrder and add the orderItems to it)... I mean I already used it that way but ... I think is kind of freacky. Isn't there a workaround or even "the right way" for something for this ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 6:19 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
DeliveredOrder should be a business state not a type... be carefull with inheritance meanings!

if you're domain model is broken, you can do this with a jdbc workaround but be carefull with first and second level cache.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 26, 2005 6:12 pm 
Newbie

Joined: Wed Sep 22, 2004 12:18 pm
Posts: 14
ok, so maybe DeliveredOrder was not a good example, but there are many valid examples where you might need to update the type of your object. For example, you might have a Client object and a VIPClient sub-class defined as a client with at least 10 orders within a year, plus a whole new set of properties. So you want this guy saved as a Client, and he then becomes a VIPClient sometime down the road...in general, you might not know the type of your client until more information is provided down the road...like age, or region, etc...but you still have to persist in the meantime.

In any case, this seems like a big and unecessary limitation in Hibernate.

One work around is to have B ( the sublass of A ) have its own .hbm.xml file, without specifying that it is in fact a sublclass of A. The mapping for B can have a discriminator, but the mapping for A will not.

So if you update the discriminator columnd via class A, you can then read it back as B. The shortcoming, of course, is maintaining a duplicate mapping file for A and B ( minus the discriminator declaration ).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 26, 2005 6:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Use JDBC to update the discrminator.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 3:24 am 
Beginner
Beginner

Joined: Thu Jun 10, 2004 6:52 am
Posts: 29
Thanks Gavin. I suppose I have to pay special atention to second level cache if I use one, isn't it ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 3:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Sure, you should perform manual eviction, using the APIs that are exposed by Hibernate for this.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 4:01 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
It just means Admin and Customer are not User subclasses in your model. So use single class with updatable "type" property.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 4:13 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
hi baliukas,
Quote:
It just means Admin and Customer are not User subclasses in your model. So use single class with updatable "type" property.


for sure, we always explain people that, but when they've made the mistake, it's good to have the workaround gavin has described.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 5:44 am 
Beginner
Beginner

Joined: Thu Jun 10, 2004 6:52 am
Posts: 29
"It just means Admin and Customer are not User subclasses in your model. So use single class with updatable "type" property."

How about when I have a Regular User and Admin as a subclass. Now that the user has hanged around the site long enough to give him admin rights. And want to give him Admin rights ? I understood what the solution is, and also I agree it's ugly and it seems against the way Hibernate should be used (I will just avoid comming to this situation so I don't have to bypass hibernate session) but aren't there real life situations when this would be needed ? To determine the type of the object should I call getType() on it ? Just asking, don't want to start a fight. Sorry


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 6:39 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
This has been discussed about 20 times already. Please search the forum.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 6:41 am 
Beginner
Beginner

Joined: Thu Jun 10, 2004 6:52 am
Posts: 29
Indeed I will, thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 7:00 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
dorel wrote:
To determine the type of the object should I call getType() on it ?

Rename it to getRole() and it will be fine.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 8:56 am 
Beginner
Beginner

Joined: Thu Jun 10, 2004 6:52 am
Posts: 29
hihi. that was funny, thanks for the humor in the last reply (the one with changing getType() in getRole()). Not the method name was a problem but my way of thinking. I'll fix the damn OO model.

After a long and painfull browsing activity (I mean really) I found this, posted by Greg Barton:

"This is an OO question, really.

The main question is: is a Teacher ALWAYS a User, and is a User ALWAYS a Person? If so, then use inheritance Person -> User -> Teacher. If a Teacher can sometimes be a User and sometimes not, use delegation. (A Teacher can contain a User object which contains all info used to access the system.)

Maybe if you thought of the User object more as a UserProfile object, that would help. (UserProfile being only the information used to access the system, not the identity of the user. (little "u" intended)) Then you have Person -> Teacher with a Person or Teacher containing a UserProfile object.
"

and after, this:

"Basically your examples fit into the "Role" (or Facet) pattern. A good example of this can be seen in security. (like operating system users, with roles like Admin, Guest, User, etc...) Each user has a different level of access defined by the roles they've been assigned, and a user can have many roles. (i.e. a user could have student, teacher, or principal level access...or all three, though that would probably be redundant)

So, in your case, you could have a Person object with potentially many (aggregated) Roles. (Teacher, Student, Principal) When the Person accessed the system, their Role list would be checked and their access level determined."

Thank you Greg Barton for thislike I said, I'll fix the damn OO model :-).
And I'll get a job in constructions maybe. not as an architect, of course :-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.