我有这样两个类之间的一个一对多的关系:在一个一对多的关系上创建列索引
class Parent {
List<Child> children;
}
class Child {
String name;
}
我使用的是.hbm.xml
文件来定义Java类映射我到桌子。这样的事情:
<class name="Parent" table="parent">
<list name="children">
<key column="parent_id" />
<list-index column="idx" />
<one-to-many class="Child" />
</list>
</class>
<class name="Child" table="child">
<property name="name" type="string" />
</class>
这工作正常。
现在我想在Child.parent_id
列上创建列索引(不是列表索引)。看来,<one-to-many>
标签不允许任何嵌套<column>
标签,所以我尝试添加一个双向关联到Child
这样的映射:
<many-to-one name="parent" insert="false" update="false">
<column name="parent_id" index="true">
</many-to-one>
但我得到一个异常:
org.hibernate.PropertyNotFoundException: Could not find a getter for parent in class Child
所以,如何在Child.parent_id
列上创建索引而不将parent
字段添加到Child
类中?我使用Hibernate 3.5.6和PostgreSQL 9.3。
你如何想象在不存在的列上有索引? 如果您想拥有双向关联或索引,您需要在子类中拥有parent_id。 – 2014-09-22 07:50:28
有一个'parent_id'列。它似乎是由''标签创建的。 –
z0r
2014-09-23 08:48:19