根据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>当我试图获得会话工厂时,我会得到一个异常,它说:
由:(map-key|composite-map-key|map-key-many-to-many|index|composite-index|index-many-to-many|index-many-to-any),(element|one-to-many|many-to-many|composite-element|many-to-any),:元素类型“org.xml.sax.SAXParseException”的内容必须匹配(元*、子选择?、缓存?、同步*、注释?、键、org.xml.sax.SAXParseException加载器?、sql?、sql?、subselect?、subselect all?、filter*)“。
有人能帮我吗?我们怎么才能用composite-map-key来使用组件作为地图的关键?
发布于 2014-09-04 07:27:36
这句话是错的:
<property name="initial" column="initial1" />它应该是:
<element type="string" column="initial1" />其他事情:记住在equals & hashCode中实现Name。
https://stackoverflow.com/questions/25659438
复制相似问题