JPA @OrderBy()通过关系表 [英] JPA @OrderBy() Through Relational Table

查看:1271
本文介绍了JPA @OrderBy()通过关系表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些有关JPA框架的帮助. 我已经读过一些有关该主题的答案,但是我无法得出任何结论.

I need some help with the JPA Framework. I've read some answers "kind of" about this topic but I couldn't reach any conclusion.

首先,这是我所追求的设计的示例.

First heres an examplo of the design i'm wooking with.

@BusinessObject
public class ClassA {

    @Column(name = "ID", nullable = false)
    private Long id;

    @OneToMany(mappedBy = "classAAttr")
    private Collection<ClassAB> classABCollection;

    //STUFF AND OTHER COLUMNS.....
}

public class ClassAB {

    @Column(name = "ID", nullable = false)
    private Long id;

    @JoinColumn(name = "TABLE_A_ID", referencedColumnName = "ID")
    @ManyToOne
    private ClassA classAAttr;

    @JoinColumn(name = "TABLE_B_ID", referencedColumnName = "ID")
    @ManyToOne
    private ClassB classBAttr;

    //STUFF AND OTHER COLUMNS.....
}

@BusinessObject
public class ClassB {

    @Column(name = "ID", nullable = false)
    private Long id;

    @Column(name = "ORDERCLAUSE", nullable = false)
    private String orderClause;

    //STUFF AND OTHER COLUMNS.....
}

因此,我需要通过ClassB中的orderClause属性对ClassA中的classABCollection进行排序,但是我找不到正确的@OrderBy()子句AND/OR位置.

So I need to order the classABCollection in ClassA by the orderClause attribute in ClassB, but I can't find the right @OrderBy() clause AND/OR location for it.

我已经阅读了一些有关Comparator界面的内容,但是不幸的是,由于业务政策的原因,我需要确保没有其他方法...

I've read some things about the Comparator Interface but, unfortunately, due to business policy, I need to be sure that there is no other way...

我应该怎么做?

先谢谢大家.

推荐答案

@OrderBy的API文档请注意:

The API docs for @OrderBy note:

属性或字段名称必须与持久名称相对应 内的相关类或嵌入类的属性或字段:

The property or field name must correspond to that of a persistent property or field of the associated class or embedded class within :

http://docs.oracle.com/javaee/6/api/javax/persistence/OrderBy.html

因此无法通过B的属性对A中的AB进行排序.

so sorting AB in A by a property of B is not possible.

替代方法是通过某种方式编写查询或在内存中进行排序.例如,Hibernate具有@Sort批注,您可以使用该批注在加载时应用内存中排序,方法是使目标Entity实现Comparable或通过指定Comparator:

The alternatives are to write a query or do an in memory sort by some means. Hibernate, for example, has an @Sort annotation which you can use to apply an in-memory sort on load, either by having the target Entity implement Comparable or by specifying a Comparator:

请参阅第2.4.6.1节:

See section 2.4.6.1:

http://docs.jboss.org/hibernate/annotations /3.5/reference/zh-CN/html_single/

这篇关于JPA @OrderBy()通过关系表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆