简体   繁体   English

从多组Hibernate投影返回单列

[英]Return single column from multi-group Hibernate projection

What I want to achieve: Select a susbset of entities that have a property value that exists in a List of values. 我要实现的目标:选择一组实体集,这些实体的属性值存在于值列表中。 This list is returned by another Query. 此列表由另一个查询返回。

In plain SQL, this can be easily achieved with subqueries. 在普通SQL中,这可以通过子查询轻松实现。 This is the Criteria query that returns the relevant values: 这是返回相关值的条件查询:

DetachedCriteria subq = DetachedCriteria.forClass(MyClass.class)
    .setProjection(
        Projections.projectionList()
            .add(Projections.alias(Projections.max("interestingVal"), "interestingVal"))
            .add(Projections.groupProperty("someval1"))
            .add(Projections.groupProperty("someval2"))
            .add(Projections.groupProperty("someval3"))
        );

I would then like to select the entities that have a value of interesting_val that was returned by the above query. 然后,我想选择上面的查询返回的值,其值为interesting_val的实体。 Like so: 像这样:

List<MyClass> resultList = sessionFactory.getCurrentSession().createCriteria(MyClass.class)
            .add(Subqueries.propertyIn("interestingVal", subq))
            .list();

Unfortunately, the subq subquery returns a List of Object-arrays with length 4, since I have 4 Projection values. 不幸的是,由于我有4个Projection值,因此subq子查询返回一个长度为4的对象数组列表。 All projection values seem to be automatically added to the SELECT clause. 所有投影值似乎都将自动添加到SELECT子句中。 This results in an SQLSyntaxErrorException: ORA-00913: too many values . 这导致SQLSyntaxErrorException: ORA-00913: too many values

How can I either tell my second Criteria query to only use the first element in the Object array or only retrieve the first column in my subquery? 我怎样才能告诉我的第二个Criteria查询只使用Object数组中的第一个元素,还是只检索子查询中的第一列?

Instead of propertyIn try propertiesIn . 代替propertyIn尝试propertiesIn

String[] vals = new String[]{"interestingVal", "someval1", "someval2", "someval3"};
List<MyClass> resultList = sessionFactory.getCurrentSession().createCriteria(MyClass.class)
            .add(Subqueries.propertiesIn(vals, subq))
            .list();

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

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