criteria

Remove dynamically an ordering to the result set in org.hibernate.Criteria

杀马特。学长 韩版系。学妹 提交于 2019-12-06 02:13:49
问题 I have a Criteria with: Criteria criteria= session.createCriteria(Libro.class).addOrder( Order.asc("ID") ); However, when I want to get the rowcount fail: criteria.setProjection(Projections.rowCount()); because there is an order by in the query. How to remove dynamically the ordering in the Criteria? I mean, I am looking for like criteria.removeOrder("ID") . 回答1: Most Criteria are actually instances of CriteriaImpl. If you cast your Criteria to CriteriaImpl and get the iterator for orders,

How to avoid unnecessary selects and joins in HQL and Criteria

无人久伴 提交于 2019-12-06 01:44:42
问题 I have been trying different combinations of HQL and Criteria and I haven't been able to avoid some unnecessary joins (in both) and some unnecessary selects (in Criteria). In our scenario, we have a @ManyToMany relationship between Segment and Application entities (navigation is from Segment to Applications). First I tried this Criteria : Application app = ... List<Segment> segments = session.createCriteria(Segment.class) .createCriteria(Segment.APPLICATIONS) .add(Restrictions.idEq(app.getId(

What is better way to work with time, using Hibernate Criteria and Oracle?

半世苍凉 提交于 2019-12-05 22:05:00
I want to select entity by Time. I have Oracle DBMS with field type DATE which contain date and time. This is my code. How to select data by Time criteria? Calendar timeCal = new GregorianCalendar(0,0,0,0,0,0); Date timeMin = timeCal.getTime(); timeCal.add(Calendar.HOUR_OF_DAY, 1); Date timeMax = timeCal.getTime(); if (minDate != null && maxDate != null) criteria.add(Restrictions.between("eventDate", minDate, maxDate)); if (onDate != null) { Calendar calendar = Calendar.getInstance(); calendar.setTime(onDate); calendar.add(Calendar.DAY_OF_MONTH, 1); criteria.add(Restrictions.between("eventDate

Math operators in Criteria queries

↘锁芯ラ 提交于 2019-12-05 21:32:29
Given the mapped hibernate class: @Entity public class MyTestClass { /* id and stuff */ private Integer aValue; private Integer bValue; } you can do the following with HQL: Query query = getCurrentSession().createQuery("select aValue * bValue from MyTestClass"); List<Double> resultList = query.list; and get the calculated result out. Is it possible to do something similar to this with the Criteria API? I still haven't found a way to use math operations with the Criteria API. We have aggregate functions like sum, avg and so on, but not the basic math operators? You can make a new property in

Issue with selecting max id rows using criteria query / hibernate query?

一个人想着一个人 提交于 2019-12-05 21:17:19
I am unable to select the rows where TestId is max for respective student, I wrote the code as follows which does not get the required output. my code is as follows, Criteria c = sessionFactory.getCurrentSession().createCriteria(student.class).setProjection(Projections.projectionList().add(Projections.property("answer"),"answer")); c.add(Restrictions.eq("surveyId",send_Survey)); //c.add(Restrictions.eq("testId", "1" )); //c.setProjection(Projection.max("testId")); c.addOrder(Order.desc("testId")); c.add(Restrictions.eq("questionid",FinalQuestionsOne)); List<String> age=c.list(); My table

Hibernate Criteria for Map key-value

自闭症网瘾萝莉.ら 提交于 2019-12-05 20:30:50
I have the following property in my hibernate entity: @ElementCollection(targetClass = String.class, fetch = FetchType.EAGER) @CollectionTable(name="FORMDATA", joinColumns = @JoinColumn(name="FORM_ID")) private Map<String, String> formData; I want to do a query with hibernate Criteria where I want to match a form with a given key-value pair, something like this: criteria.add(Restrictions.like("formdata.key", "%"+value+"%").ignoreCase()); where 'key' and 'value' are passed via method parameters. Anyone knows how this should work? For me the hibernate documentation is not clear on this. Thanks a

NHibernate: Convert an ICriteria to a DetachedCriteria

最后都变了- 提交于 2019-12-05 19:40:37
Anyone know how to convert an ICriteria into a DetachedCriteria. I need to use an existing ICriteria as part of a subquery using: .Add(Subqueries.PropertyIn("Name", myDetachedCriteriaSubquery)) Is there any way to convert an ICriteria to a DetachedCriteria. I will accept 'no' with a credible reference. Following on from mattk's answer, you can inherit DetachedCriteria to access its constructors: public class ConvertedDetachedCriteria : DetachedCriteria { public ConvertedDetachedCriteria(ICriteria criteria) : base((CriteriaImpl) criteria, criteria) { var impl = (CriteriaImpl) criteria; impl

Removing Order from NHibernate Criteria Query

情到浓时终转凉″ 提交于 2019-12-05 18:37:05
I have a criteria query that I am using to show pages of results. I also need to obtain the total count of all items. Rather than have two queries, one for paging the results and one for the count (since they are identical apart from the .AddOrder() public ICriteria StandardQuery { get { return NHibernateSesssionManager.GetSession.CreateCriteria<Person>.AddOrder("OrderProperty", Order.Desc); } public ICriteria CountQuery { get{ return StandardQuery.SetProjection(Projections.Count("ID")); } Obviously the CountQuery barfs with "Column "dbo.Person.ordercolumn" is invalid in the ORDER BY clause

Mybatis通用Mapper介绍与使用

泄露秘密 提交于 2019-12-05 17:53:18
前言 使用Mybatis的开发者,大多数都会遇到一个问题,就是要写大量的SQL在xml文件中, 除了特殊的业务逻辑SQL之外,还有大量结构类似的增删改查SQL 。而且,当数据库表结构改动时,对应的所有SQL以及实体类都需要更改。这工作量和效率的影响或许就是区别增删改查程序员和真正程序员的屏障。这时,通用Mapper便应运而生…… 什么是通用Mapper 通用Mapper就是 为了解决单表增删改查 ,基于Mybatis的插件。开发人员不需要编写SQL, 不需要在DAO中增加方法,只要写好实体类,就能支持相应的增删改查方法 。 如何使用 以MySQL为例,假设存在这样一张表: CREATE TABLE `test_table` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT '', `create_time` datetime DEFAULT NULL, `create_user_id` varchar(32) DEFAULT NULL, `update_time` datetime DEFAULT NULL, `update_user_id` varchar(32) DEFAULT NULL, `is_delete` int(8) DEFAULT NULL, PRIMARY KEY (`id

using a ParameterExpression versus a variable in JPA Criteria API

我的未来我决定 提交于 2019-12-05 16:24:11
问题 When using the JPA Criteria API, what is the advantage of using a ParameterExpression over a variable directly? E.g. when I wish to search for a customer by name in a String variable, I could write something like private List<Customer> findCustomer(String name) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Customer> criteriaQuery = cb.createQuery(Customer.class); Root<Customer> customer = criteriaQuery.from(Customer.class); criteriaQuery.select(customer).where(cb.equal