gpt4 book ai didi

java - 在 Hibernate 中通过映射表值排序

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:35:17 25 4
gpt4 key购买 nike

我有一个 @ManyToMany 映射,其中表通过映射表自引用,我们想根据实际映射表中的订单 ID 进行订购,但发现很难配置它。

我们可以在 hibernate xml 中执行它,因此很自然地假设支持在 JPA 注释中存在。有人知道我们如何对映射表中的值进行排序吗?

表格是:

wap_site_components

intid
strname
intcomponentdef
dtmcreated
intcustomer

而自引用的映射表是:

wap_site_component_relations

intid
intparent (references intid in wap_site_components)
intchild (references intid in wap_site_components)
intorder (this is the value we want to order the collection on)

在 Hibernate 注释中我们有:

@ManyToMany (fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable (name = "wap_site_component_relations",
joinColumns = {@JoinColumn (name = "intparent", referencedColumnName = "id") },
inverseJoinColumns = {@JoinColumn (name = "intchild", referencedColumnName = "id") })
public Set<WapComponent> getChildren() {
return children;
}

这是我们在 hibernate xml 中执行它的方式:

        <set
name="children"
table="wap_site_component_relations"
lazy="true"
cascade="none"
sort="unsorted"
order-by="intorder"
mutable="false"
>
<cache
usage="read-only"
/>

<key
column="intparent"
>
</key>

<many-to-many
class="se.plusfoursix.im46.data.wap.WapComponent"
column="intchild"
outer-join="auto"
/>

</set>

所以我们想使用@OrderBy 标签但是不能引用映射表中的inorder 值。有任何想法吗?谢谢。

(我们已经在代码中对子集合尝试了 @OrderBy(intorder),但没有成功)

最佳答案

您可以创建一个带注释的对象来表示链接表,然后使用@oneToMany 从父链接映射到子链接,然后映射到子链接

@Entity
public class Parent {
@id
Long id;

@OneToMany(targetEntity = ChildLink.class)
Set<ChildLink> childLinks;
}

public class ChildLink {

@Id
@OrderBy
Long orderBy;

@ManyToOne
Set<Parent> parents;

@ManyToOne
Set<Child> children;
}

只是一个粗略的例子。然后,您可以通过编程方式从 childLink 集合中生成子集合,也许是在父类的 getChildren 方法中。

关于java - 在 Hibernate 中通过映射表值排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/863246/

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