-->
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.  [ 2 posts ] 
Author Message
 Post subject: dynamic mapping of table columns to different objects
PostPosted: Mon May 01, 2006 11:25 am 
Newbie

Joined: Mon May 01, 2006 11:14 am
Posts: 1
Hi

My requirement is like this: I have a table A. Depending on value of A.col1 I want to map other columns of this table to other objects.
For example, If col1 has value 2, I want to have primary key of another table B(object B) into my A.col2 i.e. I want the object B to be part of Object A via column A.col2
If col1 hsa value 3, I want to have primary key of another table (object) into my A.col3

P.S : I already have values in column A.col1. I couldn't find any use of discriminator in this case.

Any suggestions would be appreciated.

Thanks
Bhumit


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 11:36 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
My preference for this situation is to use separate classes and separate mappings. Use the where="" attribute of the class element. Something like this:
Code:
<class name="TableAClass1" where="col1=1">
  ...
  <property name="Col1INTERNAL" column="col1"/>
  <many-to-one name="Foo" column="col2"/>
  ...
</class>

<class name="TableAClass2" where="col1=2">
  ...
  <property name="Col1INTERNAL" column="col1"/>
  <many-to-one name="Bar" column="col2">
  ...
</class>
Note that, in order to insert new rows to table A when using this method, you need to have somewhere to store the "discriminator" value (the one in the where="??" attribute). Make this a member of the class, but have the set method do nothing, and the get method returns the "discriminator" value. This makes hibernate think that it's just another value that it needs to write to the table, but doesn't expose it to the users.

There are other ways of doing this. I've never investigated them though, because this way is easy, and the overhead is tiny: just two stub methods.

_________________
Code tags are your friend. Know them and use them.


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