我有這樣兩個類之間的一個一對多的關係:在一個一對多的關係上創建列索引
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