@ElementCollection使用xml配置 [英] @ElementCollection using xml configuration
问题描述
我试图持久一个对象,其中有一组字符串到数据库,我发现我必须使用 @ElementCollection
这样做,但因为我使用 .hbm.xml
配置文件通过我的所有项目我想使用 xml
。
此问题 Hibernate,List< String> 显示了如何通过注释进行操作,此链接< a href =http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection =nofollow> http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection 提供了相关建议使用xml。但是当我试图使用< element-collection>
我的eclipse IDE不接受它,并给我一个错误的元素< class />
表示元素类的内容必须匹配...
I'm trying to persist an Object which have a set of Strings to database, I found that I must use @ElementCollection
to do that, but since I'm using .hbm.xml
configuration files through all my project I want to do it using xml
.
this question Hibernate, List<String> shows how to do it through annotations, and this link http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection provides tips to do it using xml. But when I tried to use <element-collection>
my eclipse IDE didn't accept it and gave me an error at the element <class/>
which says 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 如下所示:
and my Role.hbm.xml
is like this:
<?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>
这里是集合的最后一个映射,其余的不更改
here is the last mapping of the set, the rest is not changed
<set name="menuItems" sort="unsorted" table="menuItems" lazy="false">
<key column="itemId" />
<element column="itemName" type="string" />
</set>
推荐答案
检查文档:
- 7.2.3. Collections of basic types and embeddable objects
- 7.3.1. Sorted collections
从该来源的示例:
<set name="aliases"
table="person_aliases"
sort="natural">
<key column="person"/>
<element column="name" type="string"/>
</set>
因此答案是< element>
- 示例7.16。
< element>
使用映射文件对集合值进行标记
< element>
可用于任何类型的集合映射,例如< bag>
< set>
< list>
...
This <element>
could be used in any type of colleciton mapping, like <bag>
<set>
<list>
...
可能很有趣: Understading对于在hibernate中收集依赖对象的限制 ...
Also could be interesting: Understading the restrictions for collection of dependent objects in hibernate ...
根据更多细节,我们可以说,这里的映射将是:
Based on more details, we could say, that the mapping here would be like:
<class name="Role" table="Role">
...
<set name="menuItems" sort="unsorted" table="menuItems" lazy="false">
<key column="menuId" /> // not itemId
<element column="itemName" type="string" />
</set>
</class>
这篇关于@ElementCollection使用xml配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!