-->
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.  [ 4 posts ] 
Author Message
 Post subject: Tricky composite-id association - is this possible??
PostPosted: Thu Mar 23, 2006 9:16 pm 
Newbie

Joined: Wed Feb 15, 2006 7:51 pm
Posts: 9
Location: Sydney, Australia
Hi Folks,
I'm trying to map a legacy table structure that uses a generic lookup table for lookup values. Unfortunately the way it was implemented is not optimal and is making the mapping difficult.

Here are the lookup table columns:
- lookupid (auto-generated PK)
- type (varchar type of the lookup item. ex: 'state', 'personstatus', etc)
- lookupvalue (the value of the item - should be unique within each type of list)
- description (a label for the item. ex: 'Florida', 'closed', etc)

Now, tables that are associated with lookup only store the lookupvalue, and not the type. the type is just inferred in the code.

I was going to create a composite-id for lookup that's made up of the type and the lookupvalue since those two should be unique. Then, in the mapping when another class is associated to lookup, it could pass the lookupvalue and hard-code the appropriate type. But in the <many-to-one> you can only put <column> tags which means I can't supply a hard-coded value in place of a column.

hmmm, pretty difficult to explain let alone implement. but if anyone has run into this situation, help would be much appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 10:45 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The perfect case for the StringValuedEnumType, http://www.hibernate.org/273.html.... assuming that the lookup tables are pretty much static. If you have to allow for new lookup values often enough that creating a new enum member and redeploying your app is prohibitive, then you need to use a composite key.

If you want to continue with hard-coding, you can do that, though it's not recommended. Replace the column attribute/element with the formula attribute/element.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 11:57 pm 
Newbie

Joined: Wed Feb 15, 2006 7:51 pm
Posts: 9
Location: Sydney, Australia
thanks tenwit. I'm having trouble finding much information on the formula element (other than a brief mention in Hibernate in Action). How can I use it to supply a hardcoded value in place of a column attribute? Just like this?: <formula>status</formula>

I'm actually using a StringValuedEnumType to define the different lookuptypes in the code. In the mapping of the association, i'd like to reference one of those enum values using the formula element you mentioned. So I would end up with something like <formula>LookupType.Status</formula>. I know that on its own wouldn't work, but is there another way?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 12:14 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Pure SQL goes inside the formula attribute/element, hence its appropriateness when hardcoding stuff. Whatever you put in there is put directly into the end result where clause. There is no substitution of any kind.

So you could put "<many-to-one class="State" formula="name='Florida'"/>". Or
Code:
<many-to-one class="State">
  <formula>name like 'A%' and ((select s.population from state s where s.name = name) > 1000000)</formula>
</many-to-one>
You have to use subselect for referencing other tables, even if they're already referenced elsewhere in the mapping, because you have no way of determining what alias hibernate has assigned to the other table. It's not efficient, but it is effective.


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