使用批注在JPA中,我可以使用where子句限制子记录吗? [英] Using Annotations In JPA can I limit child records with a where clause?

查看:47
本文介绍了使用批注在JPA中,我可以使用where子句限制子记录吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的父类(时隙)中,我有一个具有@onetomany关系的EJB:

I have an EJB with an @onetomany relationship like this in my parent class (Timeslot):

@OneToMany(mappedBy = "rsTimeslots")
private List<RsEvents> rsEventsList;

我还有一个获取rsEventList的函数:

I also have a function to get the rsEventList:

public void setRsEventsList(List<RsEvents> rsEventsList) {
    this.rsEventsList = rsEventsList;
}

到目前为止,所有操作都是自动生成的.在我的视图层代码中,我可以获取一个时隙对象并执行类似timeslot.getRsEventList()的操作,并获取该时隙的所有子代.现在,我需要根据条件限制该列表.例如,我只想要状态为1的该时间段的子级事件.是否可以通过注释来执行此操作?

This was all auto generated so far. In my view-layer code I can get a timeslot object and do something like timeslot.getRsEventList() and get all children of this timeslot. Now I need to restrict that list based on a criteria. For example I only want events that are children of this timeslot with a status of 1. Is there a way to do this with annotations?

推荐答案

不在JPA中.

通常,您将使用JPQL或标准API对此执行查询.

Normally you would execute a Query for this, using JPQL or the criteria API.

某些JPA提供程序确实提供了限制关系的方法,但是我认为最好使用查询,或者在类中提供只访问列表并对其进行过滤的get/filter方法(即getStatus1Events()).

Some JPA providers do provide ways to restrict relationships, but I think you would be best off with a query, or providing a get/filter method on your class that just accesses the list and filters it (i.e. getStatus1Events()).

有关在映射上具有条件的EclipseLink示例,请参见, http://wiki.eclipse.org/EclipseLink/Examples/JPA/MappingSelectionCriteria

For an EclipseLink example of having a criteria on a mapping see, http://wiki.eclipse.org/EclipseLink/Examples/JPA/MappingSelectionCriteria

这篇关于使用批注在JPA中,我可以使用where子句限制子记录吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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