Hibernate 3.3:如何按名称映射枚举列表 [英] Hibernate 3.3: how to map list of enums by name
问题描述
我正在使用遗留代码和紧迫的期限进行项目。该应用程序使用Hibernate 3.3.1.GA和XML配置。可能可以升级到较新版本的Hibernate并集成JPA,以启用我在其他问题(例如 AttributeConverter< Value,Representation> 解决方案。 = https://stackoverflow.com/a/642805/385043> ),但我确实需要阻力最短的路径。
我有一个实体,该实体将与Java代码中以枚举表示的值具有一对多关系。我创建了一个表,该表应该捕获很多方面,如下所示:
创建表theValueMapping(
id int unsigned NOT NULL AUTO_INCREMENT,
实体int unsigned NOT NULL,
listIndex int unsigned NOT NULL,
值VARCHAR(4)NOT NULL,
PRIMARY KEY(id),
CONSTRAINT FK_theValueMapping_entity外键(实体)参考entityTable(ID),
UNIQUE UNIQUE_theValueMapping_entity_listIndex_pair(实体,listIndex),
UNIQUE UNIQUE_theValueMapping_entity_value_pair(实体,值)
);
自然地,我的实体有一个存储枚举实例列表的字段:
package com.example;
公共类TheEntity {
private List< TheValue>价值;
//其他所有东西都掉了,但是有吸气剂和设置剂
}
并假定枚举很简单:
package com.example;
公共枚举TheValue {
A,
B,
C;
}
我需要获得此映射才能通过XML Hibernate配置工作。我已经设计出的最好的应用程序可以启动应用程序,但无法加载任何记录:
<?xml version = 1.0 encoding = utf-8?>
<!DOCTYPE休眠映射公共-//休眠/休眠映射DTD 3.0 // EN
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\"> ;
< hibernate-mapping>
<类名= 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>
我该如何配置它,以便Hibernate将这些枚举值的列表从数据库加载到我的实体中?
答案实际上是 typedef
的组合我以前拒绝的方法,加上一个缺少的依赖项。
我没有意识到 org.hibernate.type.EnumType
是在Hibernate Annotations JAR中定义的,因此我添加了
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId>休眠注释< / artifactId>
< version> $ {hibernate.version}< / version>
< / dependency>
到我的POM。
然后,我重新设计了Hibernate配置,以使用 typedef
通过此类型映射我的枚举:
<?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>
<类名= 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>
感谢 grimarr 显示我需要 hibernate-annotations.jar
并 SO的迈克表示 typedef
方法应该可行。 / p>
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?
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.
这篇关于Hibernate 3.3:如何按名称映射枚举列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!