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?