In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how Hibernate queries, with certain reference value, interested friends can refer to, I hope you have a lot of gains after reading this article, let Xiaobian take you to understand.
Package name: hibernate-conditions.jar
The main extension of the hibernate query function, which relies on hibernate and hibernate related packages.
The main extension of a variable annotation as a query condition function, the entire object as an unknown query condition, to the parser to parse. The core is the use of several annotations. These comments are explained below.
1. AsCondition annotates a variable as a condition with two main fields,propertyName and symbol, where propertyName is the field specifying the hibernate entity to query. symbol indicates the operation comparator of the query, i.e."=","
< ",">"等操作.还有几个比较次要的字段: groupOrdinal 指定分组的序号,如果一个对象有多个查询条件,可以指定这些条件序号.可以在不要的查询过程中指定不同的查询序号.
ignoreNull 是否忽略null值,默认是忽略.如果不忽略null值,则在条件解析过程中遇到该查询条件的值为空,则将该条件解析为 "propertyName" is null
inherit 是否查询父类,默认是不查询父类.
2. AsAlias 查询集合,对应Criteria.createAlias()方法.只是里面有一个ignoreEmpty字段不同,该字段指定是否忽略集合为空,如果不忽略集合为空,则当该值为null的时候,解析后的查询条件为:集合 is empty
3. DesignatedProperty 该注释是用在被注释的是一个类,并指定该类中的一个属性作为查询条件的值.
4. Conversion 用于条件值的数据类型的转换.如:被查询条件值为Integter而被注释的变量为String,这时候就可以使用该注释把数据类型从String转换为Integter.除了通用类型的数据的转换还可以数据的类型的转换,把数据转换的类型ConversionType指定为Custom,注意,如果该属性指定为Custom则需要给出customDataConversionType的类型,该类型必须是继承DataConversion接口.如果没有指定,则不能进行任何的数据转换.
Conversion里还有一个属性throwDataConversionException,该属性指定如果数据转换异常,是否抛出异常,如果抛出异常则当次的查询会终止.如果出现异常但不抛出,则会忽略该查询条件.
使用也很简单:
创建一个AnnotationConditionParser对象,调用该对象的parser(Object...unkownConditionObject)方法可以指定未知道的条件实体,该方法被了接收被注释过的实体外还可以接收原来hibernate的查询条件和字符串的查询条件,如果是字符串的查询条件必须符合Criteria的查询格式,否则在hibernate解析时会有查询的异常.
AnnotationConditionParser对象中的groupLimit(int groupLimit)方法用于限定查询的分组,如果指定的分组则只解析属于该组的条件.
具体执行对条件的解析操作是createCriteria(DetachedCriteria criteria)方法,该方法把所有的条件解析成DetachedCriteria 的查询条件.接下来就是对DetachedCriteria 的操作了.由于DetachedCriteria 对象是hibernate的对象,该对象的用法相信大家比我还熟悉. 我在这里就不再解释了.
具体例子(只作为例子讲解,无法直接运行):
public class ConditionTest { /** * @param args */ public static void main(String[] args) { ConditionTest test = new ConditionTest(); test.setName("my name"); test.setSize(10); OtherClass other1 = new OtherClass(); other1.setName("name1"); test.setObjectCondition(other1); OtherClass other2 = new OtherClass(); other2.setName("name1"); test.setOther(other2); DetachedCriteria detachedCriteria = DetachedCriteria.forEntityName("UserEntity"); new AnnotationConditionParser() //限定查询的分组 .groupLimit(1) //设置查询的实体对象 .parser(test) //执行查询并填充DetachedCriteria对象 .createCriteria(detachedCriteria); // ... // 执行detachedCriteria对象的数据查询操作 } //最简单的用法 @AsCondition(propertyName="size") private int size; @AsCondition(propertyName="name", symbol=Symbol.LIKE) private String name; //指定对象里的一个属性作为查询的条件 @AsCondition(propertyName="name") @DesignatedProperty(propertyName="name") private OtherClass objectCondition; public int getSize() { return size; } public void setSize(int size) { this.size = size; } public String getName() { return name; } public void setName(String name) { this.name = name; } public OtherClass getObjectCondition() { return objectCondition; } public void setObjectCondition(OtherClass objectCondition) { this.objectCondition = objectCondition; } public OtherClass getOther() { return other; } public void setOther(OtherClass other) { this.other = other; } //使用数据类型的转换 @AsCondition(propertyName="datetime", symbol=Symbol.NE, ignoreNull=true) @DesignatedProperty(propertyName="name") @Conversion(dataConversion=ConversionType.Custom, customDataConversionType=CustomTypeConversion.class) private OtherClass other; public static class OtherClass{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } public static class CustomTypeConversion implements DataConversion{ @Override public Object doConversion(Object data) throws DataConversionException { return Date.valueOf(data.toString()); } } }感谢你能够认真阅读完这篇文章,希望小编分享的"Hibernate如何查询"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.