-->
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.  [ 9 posts ] 
Author Message
 Post subject: Flexible discriminator-value possible?
PostPosted: Mon Jun 12, 2006 10:32 pm 
Newbie

Joined: Mon Jun 12, 2006 10:04 pm
Posts: 5
I am trying to define a subclass matching a discriminator-value that can be either of 2 possible values, ie. is in ["a", "b"]. Is this possible?

E.g. in the example below, a "sportsfan" can be a "soccerFan", a "tennisFan" or BOTH. I do NOT want to create a separate object "soccerTennisFan" for the latter.


<class name="sportsFan" table="sportsfan">
..
<discriminator column="fanType" type="integer" />

..
<subclass name="soccerFan" discriminator-value="1">
<subclass name="tennisFan" discriminator-value="2">



(The corresponding descriminator value for a fan that belongs to BOTH would be 3.)


I already looked at using a formula in the discriminator, sth. like

<discriminator "case when FAN_TYPE in ("1","3") then "soccerfan" else when FAN_TYPE in ("2","3") then "tennisFan" else "otherFan" />


However, in this case if FAN_TYPE is 3 (ie. soccer AND tennis fan), he would always come out as a soccer fan only.


Another option I see would be using multiple discriminators (e.g. first = "isSoccerFan", second = "isTennisFan"), but I don't think Hibernate supports such.



Any help/ideas appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 11:24 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
Do you want to ceate 2 objects of sportsFan namely tennisfan , soccerfan if the value is 3.??

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 12:29 am 
Newbie

Joined: Mon Jun 12, 2006 10:04 pm
Posts: 5
The DB has only 1 table, ie. SPORTSFAN. SoccerFan and TennisFan both extend the SportsFan object.

A sportsfan who is both, a soccerFan and a tennisFan, is stored only once in the DB SPORTSFAN table with TYPE set to 3.

So, if value/type is 3, I want to access this dataset as a soccerFan object as well as as a tennisFan object.

Example: SPORTSFAN DB Table

ID Name Type
---------------------------------
1 John 1
2 James 2
3 Frank 3

If I query the DB/Hibernate with SELECT all SOCCERFAN, I want to get John and Frank. (Here, Frank will be treated as a soccerFan object.)

If I query the DB/Hibernate with SELECT all TENNISFAN, I want to get James and Frank. (Here, Frank will be treated as a tennisFan object.)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 2:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
use entity-name

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 4:24 am 
Newbie

Joined: Mon Jun 12, 2006 10:04 pm
Posts: 5
max,

Thanks for the suggestion. But I don't see how this solves my problem. Any further lead?


Another option that I can think of would be to use a regular expression in the discriminator-value. Anybody any idea whether such is supported? (and if so, how?)

Cheers


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 10:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
have 2 mappings for the same class, but different entity-name.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 8:05 pm 
Newbie

Joined: Mon Jun 12, 2006 10:04 pm
Posts: 5
Max, Thanks again.

If I understand you correctly, this would imply that I have to create a third type of Java object "soccerTennisFan" which I don't want to do.

Reason: There are many possible types of "fans", ie. many different combinations. Creating a separate object for each would blow up the object model.

From a screen perspective, I will always access the type=3 fan either as a "soccerFan" or as a "tennisFan" but never as a "soccerTennisFan" object. That is why I was looking for a flexible descriminator-value like descriminator-value in [1, 3].

As it seems that such is not possible with Hibernate, I probably have to use "JOINED-SUBCLASS" and create 2 additional index tables in the DB, 1 storing the soccerFan IDs, 1 storing the tennisFan IDs.

Any comment / further idea?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 14, 2006 4:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
NO - you don't need a third type. You just need to use a different entity-name. entity-name != class name.

/max

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 12:34 am 
Newbie

Joined: Mon Jun 12, 2006 10:04 pm
Posts: 5
Many thanks Max.

M


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