1

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

4

1 回答 1

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 真的没有任何意义。

于 2012-07-18T10:45:07.900 回答