-->
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: Persistent enum with referenced by multiple columns
PostPosted: Tue Jun 14, 2005 4:45 pm 
Newbie

Joined: Tue Nov 23, 2004 8:19 am
Posts: 12
Location: Santa Maria-RS, Brazil
Hi,

I had problems with persistent enums mapped to multiple columns and I'd appreciate some help. Let me explain the context of the problem.

I have to access data from a legacy db, so I cannot change the schema. It has a table for enumerations, with ID, TYPE (type of the enum, i.e. the discriminator), ITEM (the value) and DESCRIPTION. All tables reference the enums through (TYPE, ITEM) instead of ID.

I thought about implementing a PersistentEnum for the table and a subclass for eache distinct value of TYPE. And so I did, just like EnumUserType.java in http://hibernate.org/272.html. The only diference is that the entities reference the enum by two columns (however, I also did the changes to the corresponding UserType):
Code:
<hibernate-mapping package="br.ufsm.cpd.sg.rh.model">
    <class  name="Person" table="PEOPLE">
        <id name="id" column="ID">
            <generator class="native"/>
        </id>
        <property name="name" column="NAME"/>
        <property name="socialState" type="enumSocialState">
            <column name="SS_TYPE"/>
            <column name="SS_ITEM"/>
        </property>
    </class>

The enum mapping:
Code:
<hibernate-mapping package="br.ufsm.cpd.sg.enum">
    <typedef name="enumSocialState" class="br.ufsm.cpd.sg.hibernate.PersistentEnumType">
        <param name="enumClassName">br.ufsm.cpd.sg.rh.enum.SocialState</param>
    </typedef>
    <class name="PersistentEnum" table="ENUM_ITEMS" discriminator-value="0" mutable="false">
        <id name="id" column="ID">
            <generator class="native"/>
        </id>
        <discriminator type="short" column="TYPE"/>
        <property name="value" column="ITEM"/>
        <property name="description" column="DESCRIPTION"/>
    </class>
    <subclass name="SocialState" extends="PersistentEnum" discriminator-value="162"/>
</hibernate-mapping>

There are no problems concerning loading and saving of Person objects, however I cannot query object by enum. An example query would be:
Code:
from Person p where :socialState = p.socialState and :id >= p.id

For this HQL query, Hibernate'd generate the following SQL query (note the illegal expression "(person0_.SS_TYPE, person0_.SS_ITEM)"):
Code:
select person0_.ID as ID1_, person0_.NAME as NAME2_1_, person0_.SS_TYPE as SS_TYP3_1_, person0_.SS_ITEM as SS_ITE4_1_ from PEOPLE person0_ where ?=(person0_.SS_TYPE, person0_.SS_ITEM) and ?>=person0_.ID

I'm setting the value for :socialState using setParameter() with the type from Hibernate.custom(PersistentEnumType.class), as "Hibernate In Action" says.
Am I missing anything? I couldn't find an example using multiple columns to reference the enums; is this possible? Does anybody have another solution?

Thanks in advance,
Rodrigo


Top
 Profile  
 
 Post subject: Persistent enum referenced by multiple columns
PostPosted: Thu Jun 23, 2005 10:30 am 
Newbie

Joined: Tue Nov 23, 2004 8:19 am
Posts: 12
Location: Santa Maria-RS, Brazil
I gave up from trying to do that. I cannot understand why hibernate doesn't use UserType.nullSafeSet() to set the parameter values in a query... Maybe it should...
Couldn't it generate an SQL expression like the folowing and set the parameters using nullSafeSet()?
Code:
?=person0_.SS_TYPE and ?=person0_.SS_ITEM


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.