【问题标题】:@ElementCollection using xml configuration@ElementCollection 使用 xml 配置
【发布时间】:2014-11-01 10:38:10
【问题描述】:

我正在尝试将具有一组字符串的对象持久化到数据库中,我发现我必须使用@ElementCollection 来做到这一点,但是由于我在我想要的所有项目中都使用.hbm.xml 配置文件使用 xml 来做到这一点。 这个问题Hibernate, List<String> 展示了如何通过注解来做到这一点,这个链接http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection 提供了使用xml 来做到这一点的提示。但是当我尝试使用 &lt;element-collection&gt; 时,我的 Eclipse IDE 不接受它,并在元素 &lt;class/&gt; 处给了我一个错误,上面写着 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


    【解决方案1】:

    查看文档:

    以及来自该来源的示例:

    <set name="aliases"
                table="person_aliases" 
                sort="natural">
        <key column="person"/>
        <element column="name" type="string"/>
    </set>
    

    所以答案是&lt;element&gt;

    这个&lt;element&gt;可以用于任何类型的colleciton映射,比如&lt;bag&gt;&lt;set&gt;&lt;list&gt;...

    也可能很有趣:Understading the restrictions for collection of dependent objects in hibernate ...

    根据更多细节,我们可以说,这里的映射如下:

    <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>
    

    【讨论】:

    • 尝试保存角色时出现异常cannot insert null into menuItems.menuId
    • 你能给我看看最新的地图吗?我猜我们会找到的;)只要告诉我你有什么(扩展我想说的答案)
    • 我会说我明白了! Look Exception 说:cannot insert null into menuItems.menuId 但映射是 &lt;key column="itemId" /&gt; 并且它必须是 &lt;key column="menuId" /&gt;
    • 对不起,我写错了,异常说:org.hibernate.exception.ConstraintViolationException: ORA-01400: cannot insert NULL into ("HUB"."MENUITEMS"."ITEMID") 我正在使用&lt;column = "itemId"/&gt;
    • 这真的很奇怪。看,如果 Hibernate 被正确映射,这个简单的场景总是有效的。因此,请检查我的答案和文档,将其与您真正拥有的(数据库与映射)进行比较。这是非常简单的场景......(有许多这样的映射)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 2015-12-29
    相关资源
    最近更新 更多