-->
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.  [ 3 posts ] 
Author Message
 Post subject: range of values map to a hierarchy : formula? Interceptor?
PostPosted: Thu May 18, 2006 1:06 pm 
Newbie

Joined: Tue Oct 25, 2005 12:32 pm
Posts: 13
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.1.3

After merrily going along being very productive with what I know about Hibernate someone asked me if I could help them map a class hierarchy where the discriminating column is an integer and a range of values map to a particular class. So, col >= 0 and col <= 50 is ClassA and col >= 100 and col <= 250 is ClassB, etc.

I had just started looking into Interceptors and was wondering if I could use these via an onLoad to do this evaluation and manipulate the state that Hibernate loaded so that the correct class gets resolved ? or is this a formula type problem ? I used formulas in another case to resolve a situation where two fields where needed for the discriminator but I was curious about whether I could go to an Interceptor route in case someone asks me to do something for which a formula is problematic.

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 19, 2006 3:14 pm 
Newbie

Joined: Tue Oct 25, 2005 12:32 pm
Posts: 13
Here's my own answer to myself in case anybody else can use it.

I used an object derived from EnhancedUserType (this is used instead of UserType because I want to use this type as a discriminator) and used a discriminator routed through my UserDefinedType. Here's snippets of the details. The column that has the range of values is CATEGORY. That's mapped normally. I defined a discriminator with a formula of CATEGORY. That just selects the CATEGORY column along with the other "normal" columns but routes it through the UserDefinedType I created.

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="MyBaseClass" table="TEST" discriminator-value="0">
        <id name="id" type="integer">
            <column name="MYID" />
            <generator class="native" />
        </id>
       <discriminator formula="CATEGORY" type="MyUserType" />
        <property name="category" type="integer">
            <column name="CATEGORY" not-null="true" />
        </property>
    </class>
</hibernate-mapping>



In the MyUserType class I just implemented nullSafeGet to check for ranges of values and return simple values instead...something like this :

Code:
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
   throws HibernateException, SQLException {
   Number result = (Number) rs.getObject(names[0]);
   if (result == null)
      return null;
   Integer val = new Integer(result.intValue());
   if (val >= 1 and val <= 100)
      val = 1;
   else if (val >= 101 and val <= 156)
      val = 2;
   return val;
}


This lets me subclass in the xml in the standard way with a single value.

Code:
<subclass name="MyDerivedClass1" discriminator-value="1" />
<subclass name="MyDerivedClass2" discriminator-value="2" />


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 3:10 am 
Regular
Regular

Joined: Wed Mar 08, 2006 2:07 am
Posts: 50
Location: Bangalore
Thanx FM,
This is what i was looking for today, got it now. Would try it...


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