休眠:限制查询与一对多 [英] Hibernate: limit query with one-to-many

查看:99
本文介绍了休眠:限制查询与一对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有 Service 实体:

For example, I have the Service entity:

@OneToMany(fetch = FetchType.EAGER, mappedBy = "service")
public List<ServiceStatus> getServiceStatuses() {
    return serviceStatuses;
}

ServiceStatus 实体:

@ManyToOne
@JoinColumn(name = "service", nullable = false)
public Service getService() {
    return service;
}

@Column(name = "date", nullable = false)
@Temporal(TemporalType.DATE)
public Date getDate() {
    return date;
}

现在我需要查询所有 Service 对象,以便每个对象只有那些 ServiceStatus 对象,其中 ServiceStatus.date 位于 date1 date2 。也就是说,如果有10 ServiceStatus 对象具有适当的日期,则 serviceStatuses 列表将只包含10个对象,而而已。是否有可能?

Now I need to query all the Service objects so that each of it has only those ServiceStatus objects where ServiceStatus.date is between date1 and date2. That is, if there are 10 ServiceStatus objects with the proper date, the serviceStatuses list will have only those 10 objects and nothing more. Is it possible?

在此先感谢。

推荐答案

使用@Filter的问题:

I solved the problem using @Filter:

@FilterDef(name = "dateFilter", parameters = {
        @ParamDef(name = "date1", type = "date"),
        @ParamDef(name = "date2", type = "date") })
@Filter(name = "dateFilter", condition = "date >= :date1 and date <= :date2")

并将其应用于会话:

session.enableFilter("dateFilter")
                .setParameter("date1", date1)
                .setParameter("date2", date2);

顺便说一下,在使用Hibernate时,我应该如何使用查询:它是自己的机制还是原始在这种情况下,像内部连接的SQL?

BTW, when working with Hibernate, what should I use for queries: it's own mechanism or "raw" SQL like "inner join" in this case?

这篇关于休眠:限制查询与一对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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