如何为@ManyToOne关系添加索引

时间:2012-07-18 10:32:13

标签: java hibernate

我在对象中有@ManyToOne关系,在“一”方面我没有@OneToMany关系如何在这种情况下添加@IndexColumn? 我可以在@ONeToOne关系中添加@indexColumn吗?

1 个答案:

答案 0 :(得分:1)

你的问题没有多大意义。 IndexColumn用于在OneToMany或ManyToMany关联中为List的元素分配索引。你只有一个ManyToOne关联,所以根本没有列表,因此不需要索引列。

我可以想象你会想要执行一个返回父项的所有子项的查询,并希望它们放在特定索引的列表中。在这种情况下,您需要向子实体添加持久索引字段,执行查询,按索引对子项进行排序,然后自己创建索引列表:

List<Child> sortedList = 
    session.createQuery("select c from Child c where c.parent = :parent order by c.index")
           .setParameter("parent, parent)
           .list();
List<Child> indexedList = new ArrayList<Child>();
for (Child child : sortedList) {
    indexedList.ensureCapacity(child.getIndex() + 1);
    indexedList.set(child.getIndex(), child);
}

对于OneToOne关联,IndexColumn确实没有任何意义。