gpt4 book ai didi

java - 我如何将索引添加到@ManyToOne关系

转载 作者:太空宇宙 更新时间:2023-11-04 08:09:09 24 4
gpt4 key购买 nike

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

最佳答案

你的问题没有多大意义。 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 确实没有任何意义。

关于java - 我如何将索引添加到@ManyToOne关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11539349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com