criteria

HibernateTemplate进行分页

我的未来我决定 提交于 2019-11-27 04:19:35
转:如何使用HibernateTemplate进行分页功能 在使用Hibernate时,可以用 query.setFirstResult(130);//设置取值的开始位置 query.setMaxResults(10); //设置读取数据的记录条数 方便的实现分页。 但是Spring 整合 Hibernate 时候用的 HibernateTemplate 却不支持分页,这样当查询记录过多时,就会给我们带来一些麻烦,因此需要自己包装一个类进行分页,具体实现如下: 使用spring的hibernateTemplate的回调机制扩展hibernateTemplate的功能实现分页。其中HibernateCallback()是一个接口,这种回调方式也是在java中常用的方法。 /** * 使用hql 语句进行操作 * @param hql * @param offset * @param length * @return List */ public List getListForPage(final String hql, final int offset, final int length) { List list = getHibernateTemplate().executeFind(new HibernateCallback() { public Object

Hibernate Criteria for Dates

让人想犯罪 __ 提交于 2019-11-27 04:14:22
In oracle I have dates in format 17-April-2011 19:20:23.707000000 I would like to retrieve all orders for 17-04-2011. SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-YYYY"); String myDate = "17-04-2011"; Date date = formatter.parse(myDate); Criteria criteria = session.createCriteria(Order.class); Criterion restrictDate = Restrictions.like("orderDate",date); but it brings me empty result: Sebastien Lorber Why do you use Restrictions.like(... )? You should use Restrictions.eq(...) . Note you can also use .le , .lt , .ge , .gt on date objects as comparison operators. LIKE operator is not

Hibernate Criteria for “in subselect”

眉间皱痕 提交于 2019-11-27 03:28:26
问题 I'm trying to do something like this, but using Criteria instead of HQL : select user from User where user in ( select user from UserDomain where domain.id = "XXX" ) User being an entity having a one-to-many relationship to the join table UserDomain. The point here is simply to find Users that are linked to a Domain having id = "XXX". This seems like it should be very simple... but I'm having no luck so far turning up any useful docs. 回答1: The subquery is very useful in cases, that you need

sql语句对中文排序

戏子无情 提交于 2019-11-27 03:17:30
1 使用criteria排序: 需要写一个GbkOrder类添加到当前项目中,继承Order,并重写,criteria.addOrder(GBKOrder.asc(“name”)); order源码: import java.io.Serializable; import java.sql.Types; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.type.Type; /** * Represents an order imposed upon a <tt>Criteria</tt> result set * @author Gavin King */ public class Order implements Serializable { private boolean ascending; private boolean ignoreCase; private String propertyName; public String toString() { return propertyName + ' ' + (ascending

上手mongodb

牧云@^-^@ 提交于 2019-11-27 02:49:23
上手MongoDB MongoDB 是一个跨平台的,面向文档的数据库, 如果你了解spring-data-jpa的使用, 那么恭喜你,你已经可以使用mongodb做开发了 使用这种类型的数据库还是挺方便的,最小的存储单位是一个文档,但是文档有什么字段,有多少字段它都不关心,而mysql这样的典型的关系型数据库,开发之前得把表设计的明明白白的,说不定还得预留几个字段以备不时之需,因为后续再改就麻烦了 。它支持的数据结构非常松散,是类似 JSON 的 BSON 格式,因此可以存储比较复杂的数据类型。 体系结构 MongoDB Mysql database database collection 数据表 document 表中的一行记录 一个MongoDB实例支持多个database并存,同时一个database中可以包含多个collection,所以大家都说它是介于关系数据库和非关系数据库之间,因为它的组成结构真的特别像关系型数据库 支持的数据类型 数据类型名 BSON null {"XXX":null} 布尔值: {"XXX":true/false} int {"XXX":NumberInt("1")} Long {"XXX":NumberLong("1")} 字符串 {"XXX":"啊哈哈哈"} 日期 {"XXX":new Date()} 正则 {"XXX":null} 布尔值:

How do add NOLOCK with nHibernate?

无人久伴 提交于 2019-11-27 01:01:29
How do you add NOLOCK when using nhibernate? (criteria query) AntonioR SetLockMode(LockMode.None) or connection.isolation ReadUncomitted does NOT append a NOLOCK to your queries. Ayende goes into the correct answer on his blog : If you're using <sql-query> you can do the following: <sql-query name="PeopleByName"> <return alias="person" class="Person"/> SELECT {person.*} FROM People {person} WITH(nolock) WHERE {person}.Name LIKE :name </sql-query> Note the WTIH(nolock) appended to the FROM clause. cbp I'll explain how to do this so that you can add NOLOCK (or any other query hints), whilst

Complex queries with JPA Criteria builder

蓝咒 提交于 2019-11-27 00:12:03
问题 Can someone suggest me how to build up the following query using JPA Criteria builder API? SELECT id,status,created_at from transactions where status='1' and currency='USD' and appId='123' order by id It's better if I can find a solution which creates dynamically based on the parameters given as a Map<String,String> using metamodel classes or any other way. 回答1: It's like this (without metamodel): Map<String, Object> params = ...; CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery

Avoiding secondary selects or joins with Hibernate Criteria or HQL query

假如想象 提交于 2019-11-26 23:18:23
问题 I am having trouble optimizing Hibernate queries to avoid performing joins or secondary selects. When a Hibernate query is performed (criteria or hql), such as the following: return getSession().createQuery(("from GiftCard as card where card.recipientNotificationRequested=1").list(); ... and the where clause examines properties that do not require any joins with other tables... but Hibernate still performs a full join with other tables (or secondary selects depending on how I set the

How to get distinct results in hibernate with joins and row-based limiting (paging)?

依然范特西╮ 提交于 2019-11-26 21:22:29
I'm trying to implement paging using row-based limiting (for example: setFirstResult(5) and setMaxResults(10) ) on a Hibernate Criteria query that has joins to other tables. Understandably, data is getting cut off randomly; and the reason for that is explained here . As a solution, the page suggests using a "second sql select" instead of a join. How can I convert my existing criteria query (which has joins using createAlias() ) to use a nested select instead? You can achieve the desired result by requesting a list of distinct ids instead of a list of distinct hydrated objects. Simply add this

Using hibernate criteria, is there a way to escape special characters?

隐身守侯 提交于 2019-11-26 20:14:43
问题 For this question, we want to avoid having to write a special query since the query would have to be different across multiple databases. Using only hibernate criteria, we want to be able to escape special characters. This situation is the reason for needing the ability to escape special characters: Assume that we have table 'foo' in the database. Table 'foo' contains only 1 field, called 'name'. The 'name' field can contain characters that may be considered special in a database. Two