-->
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: usertype vs entitypersister
PostPosted: Thu Jan 26, 2006 4:59 pm 
Newbie

Joined: Tue Mar 16, 2004 11:41 am
Posts: 6
binding, say, an enum to a usertype is performed in the context of a property. i see this as a flaw. everywhere you have a property of the given type, you must repeat the usertype declaration/binding. there should be one place where i can say, for example: "for this enum type, use this usertype"

also, doesn't the generalization of a usertype outside the context of a single property equate to an entitypersister?

i'm trying to wrap my head around why the usertype api and the persister api are so different from one another..and why there simply isn't a single api for both uses (or, why are they not interchangeable).

concrete illustration:
>>
say i have an enum: Color: GREEN, RED, BLUE;
i create a usertype for this enum: ColorUserType

if i have a Car class/entity with a color property,
all is well:
<class name="Car" ..>
<property name="color" type="ColorUserType" />
..
</class>

now what if i throw in a little polymorphism:
what if i have two types of Color, one
that i can easily represent as a color enum, and
another that is its own entity:

interface IColor
enum Color implements IColor
class ComplexColor implements IColor
(ComplexColor has its own mapping file)

now, the car relationship becomes polymorphic:

<class name="Car" ..>
<any name="color" id-type="long" />
<column name="colortype" />
<column name="colorid" />
</any>
</class>

there are two problems here:
if the color is a ComplexColor, then all's well. colorid
is part of the composite foreign key used to relate the car
to the color, and an id-type of 'long' fits.
if on the other hand, it's an enum type and
its usertype strategy is to use the enum's
name() which is a string, then we have to do some more work:

replacing the id-type like this will do the job:
id-type="MyPolymorphicUserType"

the usertype implementation can be made to disambiguate
how to work with the colorid depending on whether it's a
foreign key to ComplexColor or simply a serialization of
an enum.

the problem i run into is that i get the error:
no entitypersister for my enum

that makes sense. by going with the <any>, i have no spot to
actually specify that the mechanism for persisting the
polymorphic property (should it turn out to be an enum)
is via my ColorUserType.

i have resolved this issue by implementing an entitypersister
for the enum. then everything works like a charm.

<< end of illustration

so back to the original premise:
1. why can i use a usertype for a non-polymorphic property but
not for a polymorphic one?
2. usertype and entitypersiter do appear to be interchangeable
or at least closely related concepts.

i believe we might be able to merge the two, do away with usertype altogether, and provide a simpler mechanism for customizing how certain types should be persisted.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 5:48 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
usertype is for *values*
entitypersisters are for *entities*

Two *very* important things to know the difference between.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 6:46 pm 
Newbie

Joined: Tue Mar 16, 2004 11:41 am
Posts: 6
A. take an enumeration, i.e. a type with a finite set of values.
for this enumeration, we define a usertype that serializes
the value as enum.name() (a string).

given two entities, each with a property of that enum type,
i should not have to specify 'type="..."' twice


B. there is a gray area for a polymorphic relationship where
one subtype could be a value while the other is an entity

see my example about the Color inheritance hierarchy where
one subclass is a value type and the other is an entity type.

what do you do in these cases?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 6:56 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
<property> is for values
<any> is for entities

two different things.

if you want a "polymorphic" property, why don't you just make your usertype polymorphic ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 8:10 pm 
Newbie

Joined: Tue Mar 16, 2004 11:41 am
Posts: 6
first of all, thank you for taking the time to read and reply to my questions/comments. you guys are outstanding.

to answer your question:
'if you want a polymorphic property, why don't you just make your usertype polymorphic'

what i have is not a polymorphic property. it's half/half.

here's an example (not a real example, but it should do):

interface IColor { ... }

two implementations of that interface:
enum ColorProperty implements IColor { GREEN, BLACK, BLUE; }
class ComplexColor implements IColor
{
int redComponent, greenComponent, blueComponent;
// with corresponding getters and setters
}

the enum, i persist with a UserType
the class (complexcolor) i persist with a hbm.xml mapping file.

no problems so far.

the problem presents itself when i have another class, Car, that has an association to an IColor interface, named "carColor".

the question is: is "carColor" a value (mapped with 'property')
or an entity (mapped with 'any')

the answer is, of course: it depends. sometimes it's a value and sometimes it's an entity.

i have solved this problem by telling hibernate the latter: it's an entity, and supplying an EntityPersister for the enum.

have you ever come across a similar modeling situation. how did you solve it?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 8:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
i used a usertype when it was a value and a entitypersister if it was an entity....i don't see the problem.

a class can be used both as a value and a entity - no problem.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: How does one make a usertype polymorphic?
PostPosted: Fri Aug 03, 2007 11:17 am 
Newbie

Joined: Fri Aug 03, 2007 11:12 am
Posts: 1
max,

this is a really old message, and many forum and jira questions seem to indicate this can't be done.

did you have something in mind when you said "make your usertype polymorphic"?

I would like to model an inheritance tree of "states" as a <component>. So the following types would be polymorphically mappable:
interface State { void doSomething(Domain obj); }

interface Open extends State {}
interface Closed extends State {}

class Created implements Open { void doSomething(Domain obj) {...} }
class OnHold implements Open { void doSomething(Domain obj) {...} }
...

class Completed implements Closed { void doSomething(Domain obj) {...} }
class Cancelled implements Closed { void doSomething(Domain obj) {...} }
...

I want polymorphic queries to work, so that I can select all tasks in the open state: "select task from Task task, Open state where task.state=state"

Thanks



max wrote:
<property> is for values
<any> is for entities

two different things.

if you want a "polymorphic" property, why don't you just make your usertype polymorphic ?


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.