0

I am working on a project with legacy code and a tight deadline. This application uses Hibernate 3.3.1.GA and XML configurations. It MIGHT be possible to upgrade to a newer version of Hibernate and to integrate JPA to enable the AttributeConverter<Value, Representation> solution I have seen in other questions (like this one), but I really need the path of shortest resistance.

I have an entity that will have a one-to-many relationship with a value that is represented in my Java code as an enum. I have created the table that is supposed to capture the many side as follows:

CREATE TABLE theValueMapping (
    id int unsigned NOT NULL AUTO_INCREMENT,
    entity int unsigned NOT NULL,
    listIndex int unsigned NOT NULL,
    value VARCHAR(4) NOT NULL,
    PRIMARY KEY (id),
    CONSTRAINT FK_theValueMapping_entity FOREIGN KEY (entity) REFERENCES entityTable (id),
    UNIQUE UNIQUE_theValueMapping_entity_listIndex_pair (entity, listIndex),
    UNIQUE UNIQUE_theValueMapping_entity_value_pair (entity, value)
);

Naturally, my entity has a field that stores a list of the enum instances:

package com.example;
public class TheEntity {
    private List<TheValue> theValues;
    // everything else dropped, but there are getters and setters
}

And assume the enum is simple:

package com.example;
public enum TheValue {
    A,
    B,
    C;
}

I need to get this mapping to work through XML Hibernate configuration. The best I have been able to devise lets the application launch but fails to load any records:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.example.TheEntity" table="entityTable" lazy="false">
        <list name="theValues" table="theValueMapping">
            <key column="entity" />
            <list-index column="listIndex" />
            <element
                column="value"
                length="4"
                type="com.example.TheValue"
                not-null="true"
            />
        </list>
    </class>
</hibernate-mapping>

How can I configure this so Hibernate will load a list of these enum values into my entity from the database?

sadakatsu
  • 1,255
  • 18
  • 36

1 Answers1

1

The answer turned out to be a combination of a typedef approach I had previously rejected plus an inclusion of a missing dependency.

I had not realized that org.hibernate.type.EnumType is defined in the Hibernate Annotations JAR, so I added

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>${hibernate.version}</version>
</dependency>

to my POM.

Then, I reworked my Hibernate configuration to use a typedef to map my enum through this type:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <typedef name="TheValue" class="org.hibernate.type.EnumType">
        <param name="enumClass">com.example.TheValue</param>
        <param name="type">12</param>
    </typedef>
    <class name="com.example.TheEntity" table="entityTable" lazy="false">
        <list name="theValues" table="theValueMapping">
            <key column="entity" />
            <list-index column="listIndex" />
            <element
                column="value"
                length="4"
                type="TheValue"
                not-null="true"
            />
        </list>
    </class>
</hibernate-mapping>

My thanks to grimarr from the Hibernate forums for showing that I needed hibernate-annotations.jar and to Mike from SO for showing that the typedef approach should work.

sadakatsu
  • 1,255
  • 18
  • 36