简体   繁体   English

休眠:使用集合中的属性值创建HQL查询

[英]Hibernate: create HQL Query with Attribute values from Sets

I want to create a HQL Query that can access Attributes of a Set of spezified Objects, let me explain via a short example : 我想创建一个HQL查询,它可以访问一组特定对象的属性, 让我通过一个简短的示例进行说明

Class Organization 班级组织

public class Organization ...{
    private int orgid;
    private Set<DomainValue> languages = new HashSet<language>(0);
    private Set<Address> adresses = new HashSet<Address>(0);
    ...
}

Class Address 班级地址

public class Address  implements java.io.Serializable {
   private int addressId;
   private String city;
   private String postalCode;
   private String streetName;
   private String houseNumber;
   ...
}

Language 语言

public class Orgunitlanguage  implements java.io.Serializable {
   private int orgLanguageId;
   private Orgunit orgunit;
   private Integer languageCd;
   ...
}

These examples are code snippets of working hibernate POJOs. 这些示例是工作的休眠POJO的代码段。 So i have an organization that can have multiple addresses and languages. 所以我有一个可以具有多种地址和语言的组织。

I want the user to specify the search criteria, but limit them to one of each kind, so only one language, one postalcode etc. 我希望用户指定搜索条件,但将其限制为每种搜索条件中的一种,因此仅一种语言,一种邮政编码等。

lets say the user wants english organizations with a housenumber 22 假设用户想要门牌号为22的英语组织

so i would build a hql query like this: 所以我会像这样建立一个hql查询:

"from organization o where o.languages.languageCd = 1 AND o.addresses.housenumber = 22"

Well and that dosen't work (illegal syntax) how do i access these Sets in the right way? 好吧,那不起作用(非法语法),我如何以正确的方式访问这些Set? Keep in mind i want to access a specific attribute and not just the whole object (which is really easy). 请记住,我要访问特定的属性,而不仅仅是整个对象(这很容易)。

I can't seem to find a documentation that i understand so a little explaination would be nice. 我似乎找不到我能理解的文档,因此进行一些解释将是不错的。

Proper way to query on collections would be like this 查询集合的正确方法是这样的

from Organization o join o.languages l join o.addresses a where l.languageCd = 1 AND a.housenumber = 22

However, this query will return any organization that has at least one language with languageCd = 1 and at least one address with housenumber = 22 . 但是,此查询将返回具有至少一种语言( languageCd = 1 housenumber = 22 languageCd = 1至少一个地址( housenumber = 22任何组织。 It will not filter out the languages and addresses that don't fit the criteria. 它不会过滤掉不符合条件的语言和地址。 Check this answer for a little more explanation on this. 检查此答案对此有更多解释。

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

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