简体   繁体   English

如何在JPA的左联接查询中限制为1个项目?

[英]How to limit to 1 item on Left join query with JPA?

Here's my use case : 这是我的用例:

public class User extends Model {}

public class TableA extends Model {
    @OneToMany
    public List<TableB> tableBs;

    @ManyToOne
    public AnObject anObject;
}

public class TableB extends Model {
    @ManyToOne
    public TableA tableA;

    @ManyToOne
    public User user;

    public String status;
}

The user get a list of TableA entries based on an instance of AnObject. 用户将基于AnObject实例获得TableA条目的列表。 I'd like to show this user if TableA is new or not. 我想向这个用户展示TableA是否是新的。

(note that TableB is acting like an:n table for User and TableA, I can't integrate it to TableA) (请注意,TableB的行为类似于User和TableA的:n表,我无法将其集成到TableA中)

To say if it is new, it depends on TableB. 要说它是否是新的,取决于TableB。 For that, I have two options : 为此,我有两个选择:

  • Either creating TableB when I create TableA, but with the status at NEW or No 创建TableA时创建TableB,但状态为NEW或No
  • Or not creating TableB for that user. 还是不为该用户创建TableB。 If the link is missing for that user, that mean it's new. 如果该用户缺少该链接,则表示它是新链接。 (doing it, I could remove the column status) (这样做,我可以删除列状态)

(actions depends on "new", it's not just a label to show). (动作取决于“新”,而不仅仅是显示的标签)。

Now, in my JPA request, how can I indicate if, for that member, TableA entry is new or not ? 现在,在我的JPA请求中,如何指示该成员的TableA条目是否为新成员?

The simple way would be to get the list of TableA, and then go through the List to check if there is the User in it (a method like isUserInTableB(User user) ) 最简单的方法是获取TableA的列表,然后遍历该列表以检查其中是否包含User(类似isUserInTableB(User user)

But is it possible to do it with JPA, for example by having only one item in the List, that would be the User, or empty if this user is not linked ? 但是,是否可以使用JPA做到这一点,例如通过在列表中仅包含一个项目(即用户),或者如果未链接此用户则为空?

(I hope I'm clear, it's not so much in my head :/) (我希望我很清楚,这不是我想的那么多:/)

Thank you really much for your help, I appreciate! 非常感谢您的帮助,谢谢!

The tableBs list is an attribute not a result of a query so you can't load it partially. tableBs列表是一个属性,而不是查询的结果,因此您不能部分加载它。

What you can do with one query is retrieve the tableB instance based on the tableA instance and the user instance. 使用一个查询可以执行的操作是基于tableA实例和用户实例检索tableB实例。 Something like 就像是

public static TableB findByTableAAndUser(TableA, tableA, User user) {
    return find("select tableB from TableB tableB where tableB.tableA=? and tableB.user=?", tableA, user).first();
}

Another way, if you want to have an "isNew" method on a tableA instance is to fetch the tableBs collection when you load tableA and juste iterate through tableBs instances in the isNew method. 另一种方法是,如果要在tableA实例上使用“ isNew”方法,则是在加载tableA并在isNew方法中遍历tableBs实例时获取tableBs集合。 Something like 就像是

public boolean isNew(User user) {
    for (TableB tableB : tableBs) {
        if (tableB.user.equals(user)) {
            return true;
        }
    }
    return false;
}

your findByIdWithTableBs method could be 您的findByIdWithTableBs方法可能是

public static TableA findByIdWithTableBs(Long id) {
    return find("select tableA from TableA tableA fetch join tableA.tableBs where tableA.id=?", id).first();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM