-->
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: Discriminator formula -- behaviour when saving new objects?
PostPosted: Wed Aug 03, 2005 2:22 am 
Newbie

Joined: Wed Aug 03, 2005 2:13 am
Posts: 16
Hibernate version:
HB3

Hi all,

I am trying to use a discriminator with a formula. I can see how the formula is used to evaluate what class to instantiate, but what is supposed to happen in the reverse direction, when saving a new persistant class?

example:
<discriminator
type="java.lang.String"
formula="case when Org_Type in ('A', B') then 'A' else 'C' end"
/>

If I try to persist an instance of the sub-class that should have a discriminator value of 'C', how would hibernate know to set the Org_Type column? I tried adding in the column attribute to the <discriminator> tag, but all examples I've seen either use "formula" or "column", but not both.

The problem I am getting when saving new objects is: cannot insert the value NULL into column 'org_type'. Hibernate does not seem to be populating the discriminator. Everything works fine if I don't use the formula, but I need this to support multiple discriminator values for one of my sub-classes.

I will post more details (mapping files, etc) if needed, but I would just like a general indication of whether this is supposed to work or not.

thanks very much,
Daniel.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 2:41 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
<property name="orgType" column="org_type"/>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 3:03 am 
Newbie

Joined: Wed Aug 03, 2005 2:13 am
Posts: 16
Thanks Christian, I knew I had to be missing something simple for such a fundamental thing to not be working :)

I assume you are saying that when using a discriminator with a formula, I need to expose the discriminator property and set it manually before saving the persistant objects?

When I use a discriminator without a formula, I don't need to do that, I know that for sure.

Is this correct? It might be worth an explicit mention in the online docs.

thanks again,
Daniel.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 4:44 am 
Newbie

Joined: Wed Aug 03, 2005 4:34 am
Posts: 1
Thanks , all

I met same problem when use formula in discriminator.

I will try it by christian's suggestion.

-Vincent


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 5:56 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Uh guys, I don't know what you think discriminator formula does. But take a step back and ask yourself: If discrimination is based on literal values that can be expressed using discriminator-value attributes in subclass mappings, why are you trying to use a complex expression (formula) to calculate it? You don't need formula at all then.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 5:57 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
And of course you have to maintain whatever fields you will use in a formula yourself.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 9:47 am 
Newbie

Joined: Wed Aug 03, 2005 2:13 am
Posts: 16
Yes, my discriminator values are simple literals, but one of my subclasses can have one of two valid discriminator values, which (I *think*) means I need to use a formula (or creating an extra redundant subclass may be another option).

It may seem obvious to you guys that using a formula means having to maintain the discriminator values ourselves, but the docs don't mention anything about this at all when moving from simple discriminators to formula-based ones.

Of course once you think about it, it is very obvious, because Hibernate would have no way of knowing how to maintain the discriminator, which is why I tried using both the column and formula attributes.

Regardless, thanks heaps for the help, I got exactly the info i was after :)

Daniel.


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.