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