【发布时间】:2014-11-01 10:38:10
【问题描述】:
我正在尝试将具有一组字符串的对象持久化到数据库中,我发现我必须使用@ElementCollection 来做到这一点,但是由于我在我想要的所有项目中都使用.hbm.xml 配置文件使用 xml 来做到这一点。
这个问题Hibernate, List<String> 展示了如何通过注解来做到这一点,这个链接http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection 提供了使用xml 来做到这一点的提示。但是当我尝试使用 <element-collection> 时,我的 Eclipse IDE 不接受它,并在元素 <class/> 处给了我一个错误,上面写着 the contents of element class must match ...
我的课就是这样
public class Role {
private Long id;
private Integer version;
private String name;
private Set<String> menuItems;
/** getters and setters **/
而我的Role.hbm.xml 是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.element.collection.beans">
<class name="Role" table="Role">
<id name="id" type="java.lang.Long" column="id">
<generator class="org.hibernate.id.TableHiLoGenerator">
<param name="table">HibernateUniqueKey</param>
<param name="column">NexHiValue</param>
</generator>
</id>
<version name="version" column="Version" />
<property name="name" column="name" type="java.lang.String"
not-null="true" length="128" />
<element-collection name="menuItems">
<collection-table>menuItems</collection-table>
</element-collection>
</class>
更新:
这里是集合的最后一个映射,其余不变
<set name="menuItems" sort="unsorted" table="menuItems" lazy="false">
<key column="itemId" />
<element column="itemName" type="string" />
</set>
【问题讨论】:
标签: java hibernate collections