Reputation: 8595
I have a one-to-many relationship between two classes like this:
class Parent {
List<Child> children;
}
class Child {
String name;
}
I'm using an .hbm.xml
file to define my mapping from Java classes to tables. Something like this:
<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>
That works fine.
Now I want to create a column index (not a list index) on the Child.parent_id
column. It seems that the <one-to-many>
tag doesn't allow any nested <column>
tags, so I tried adding a bidirectional association to the Child
mapping like this:
<many-to-one name="parent" insert="false" update="false">
<column name="parent_id" index="true">
</many-to-one>
But I get an exception:
org.hibernate.PropertyNotFoundException: Could not find a getter for parent in class Child
So, how can I create an index on the Child.parent_id
column without adding a parent
field to the Child
class? I'm using Hibernate 3.5.6 and PostgreSQL 9.3.
Upvotes: 3
Views: 4893
Reputation: 1910
At first I didn't get it what you are looking for, but I think I figure it out.
You defined the list-index
which is used to save the index of the java.util.List and not the parent_id of the Child. So you are looking how to create real database index. That can be easy done by annotation like this;
@OneToMany(cascade = { CascadeType.ALL })
@JoinColumn(name="parent_id")
@Index(name = "idx_parent_id", columnNames = "parent_id")
private List<Child> images = new ArrayList<Child>();
And in xml configuration would look like,
<list name="children">
<key>
<column index="parent_id_idx" name="parent_id"/>
</key>
<list-index column="idx"/>
<one-to-many class="hr.winkme.server.model.entities.Child"/>
</list>
Hope it helps.
Upvotes: 4