OneToMany属性值上的Hibernate Criteria条件或子查询

时间:2014-06-05 16:08:48

标签: java hibernate criteria

我的应用程序的一个组件记录用户使用http URL发送的参数。映射如下:

public class ActivityLog {

    @Column(name = "id")
    private Long id;

    @OneToMany(mappedBy="activityLog", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    protected List<ActivityLogParameter> activityLogParameters = new ArrayList<ActivityLogParameter>();
}

public class ActivityLogParameter {

    @Column(name = "id")
    private Long id;

    @Column(name = "key", length=10)
    protected String key;

    @Column(name = "value", length=50)
    protected String value;

    @ManyToOne(fetch = FetchType.LAZY, cascade={CascadeType.MERGE})
    @JoinColumn(name="activity_log_id")
    protected ActivityLog activityLog;
}

假设每个URL总是传递2个参数:U和L

我需要使用hibernate的Criteria(从规范中强制要求)创建一个查询,以便它返回所有ActivityLogs,其中两个参数匹配某个值。即:U = aaa,L = bbb

我试过这样:

Criteria criteria = getCurrentSession().createCriteria(ActivityLog.class, "al");
// create alias
criteria = criteria.createAlias("activityLogParameters", "alp",JoinFragment.LEFT_OUTER_JOIN);

// create transformer to avoid duplicate results
criteria = criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
criteria = criteria.setFetchMode("al.activityLogParameters", FetchMode.SELECT);

//filters
criteria = criteria.add(Restrictions.or(Restrictions.eq("alp.value", UValue), Restrictions.ne("alp.key", "L")));
criteria = criteria.add(Restrictions.or(Restrictions.eq("alp.value", LValue), Restrictions.ne("alp.key", "U")));

但在这里我被困住了。我尝试添加像distinct和group by这样的投影,但这还不足以得到正确的结果。

我也试图将此条件用作子标准,因此要计算任何ActivityLog的行数并仅保留具有count(*)= 2的记录(所有参数都符合条件)但我可以找不到子查询的方法。

有关如何解决上述问题的任何想法?在SQL中我会做这样的事情:

select activity_log_id from (
    select count(*) as ct, activity_log_id 
    from activity_log_parameter alp inner join activity_log al on alp.activity_log_id=al.id 
    where (alp.value='visitor' or alp.key<>'U') 
        and (alp.value='room1' or alp.key<>'L') 
    group by activity_log_id
) as subq
where subq.ct = 2

由于

1 个答案:

答案 0 :(得分:0)

使用子查询解决

DetachedCriteria subquery = DetachedCriteria.forClass(ActivityLogParameter.class, "alp")
        .createAlias("activityLog", "al",JoinFragment.LEFT_OUTER_JOIN)
        .setProjection(Projections.projectionList()
                .add(Projections.count("id"), "alpId"));

subquery = subquery.add( Property.forName("al.id").eqProperty("mainAl.id") );

subquery = subquery.add(Restrictions.or(Restrictions.eq("alp.value", UValue), Restrictions.ne("alp.key", "L")));
subquery = subquery.add(Restrictions.or(Restrictions.eq("alp.value", LValue), Restrictions.ne("alp.key", "U")));

Criteria criteria = getCurrentSession().createCriteria(type, "mainAl");

criteria = criteria.add(Subqueries.eq(new Long(2), subquery));