如何在<composite-map-key>休眠中使用



根据Hibernate文档,我们可以使用<composite-map-key>标记将组件用作映射的键。所以我试着创建一个程序来了解它是如何工作的,但却被如何创建映射文件所困扰。

我已经声明了一个pojo类:

Person.java

public class Person {
    private java.util.Date birthday;
    private Map<Name, String> someNames = new HashMap<Name,String>();
    private int id;
    // Setters & Getters   
}

组件类别Name.java:

public class Name {
    String first;
    String last;
  // Setters & Getters    
}

我试图创建映射文件,但我不确定它应该是什么样子,下面是不正确的文件:

<hibernate-mapping>
    <class name="Person" table="test_person">
        <id name="id" column="pid" type="int">
            <generator class="increment" />
        </id>
        <property name="birthday" type="date" />
        <map name="someNames" table="test_person_names">
            <key column="person_id"></key>
            <composite-map-key class="Name">
                <key-property name="first" column="first1"></key-property>
                <key-property name="last" column="last1"></key-property>
            </composite-map-key>
            <property name="initial" column="initial1" />
        </map>
    </class>
</hibernate-mapping>

当我试图获得会话工厂时,我得到了一个异常,说:

由以下原因引起:org.xml.sax.SAXParseException:元素类型的内容"map"必须匹配"(meta*,subselect?,cache?,synchronize*,comment?,key,(映射键|复合映射键|映射键多对多|索引|复合索引|索引多对多|index多对任意),(元素|one-to-many|many-to-many|复合元素|many-to任意),loader?,sql insert?,sql update?,sql delete?,sql delete all?,filter*)"。

有人能帮我如何使用composite-map-key来使用组件作为地图的密钥吗?

此行错误:

<property name="initial" column="initial1" />

应该是:

<element type="string" column="initial1" />

另一件事:记住实现equals&CCD_ 6在CCD_。

最新更新