criteria

Hibernate--Criteria Query and DetachedCriteria

北城余情 提交于 2019-11-27 09:14:09
Criteria Query通过面向对象的设计,将数据查询条件封装为一个对象。简单来说,Criteria Query可以看作是传统SQL的对象化表示,如: Criteria criteria=session.createCriteria(TUser.class); criteria.add(Expression.eq("name","Erica")); criteria.add(Expression.eq("sex",new Integer(1)); 这里的criteria实例本质上是对SQL“select * from t_user where name='Erica' and sex=1”的封装。 Hibernate在运行期会根据Criteria中指定的查询条件生成相应的SQL语句。 Criteria查询表达式 Criteria本身只是一个容器,具体的查询条件要通过Criteria.add方法添加到Criteria实例中。 方法 描述 Expression.eq 对应SQL “field=value”表达式 如:Expression.eq("name","Erica") Expression.allEq 参数为一个Map对象,其中包含了多个属性-值对应关系。相当于多个Expression.eq关系的叠加 Expression.gt 对应SQL“field>value”表达式

SQL syntax term for 'WHERE (col1, col2) < (val1, val2)'

丶灬走出姿态 提交于 2019-11-27 09:09:36
As my question states, I would like to know what we call types of queries with that type of condition in the WHERE clause, i.e.: SELECT * FROM mytable WHERE (col1, col2) < (1, 2); In other words: Give me all records where col1 is less than '1' or if it equals '1' then col2 must be less than '2' - and none of the values are NULL. I really like this type of syntax, but don't know what the naming convention is on how to refer to this type of condition. It looks like a tuple conditional but that name is not giving me anything from my searches. My question stems from needing to know what this

UNION to JPA Query

不打扰是莪最后的温柔 提交于 2019-11-27 09:03:25
Is it possible to query "UNION" in JPA and even "Criteria Builder"? I'm looking for examples, but so far i got no result. Does anyone have any examples of how to use it? Or would that be with native sql? Masudul SQL supports UNION, but JPA 2.0 JPQL does not. Most unions can be done in terms of joins, but some can not, and some are more difficult to express using joins. EclipseLink supports UNION. Depending on the case, one could use sub queries, something like: select e from Entity e where e.id in ( select e.id from Entity2 e2 join e2.entity e where e2.someProperty = 'value' ) or e.id in (

How do I escape a LIKE clause?

对着背影说爱祢 提交于 2019-11-27 07:05:48
问题 The code we're using is straightforward in this part of the search query: myCriteria.Add( Expression.InsensitiveLike("Code", itemCode, MatchMode.Anywhere)); And this works fine in a production environment. The issue is that one of our clients has item codes that contain % symbols which this query needs to match. The resulting SQL output from this code is similar to: SELECT ... FROM ItemCodes WHERE ... AND Code LIKE '%ItemWith%Symbol%' Which clearly explains why they're getting some odd

How to make a JPA query with LEFT OUTER JOIN

守給你的承諾、 提交于 2019-11-27 07:01:02
问题 Based on these two tables (and their corresponding entities) : profiles(id, email, name) items(id, profile_id, rank, price) Profile(email, name) Item(profile, rank, price) I have to list all the profiles, ordered by the best rank of their items (it's a "top profile" list in fact). Here is the SQL request like you can execute it in PHPMyAdmin for example: SELECT AVG(i.rank) AS rank, p.* FROM profiles AS p LEFT OUTER JOIN items AS i ON p.id = i.profile_id GROUP BY p.id ORDER BY rank DESC; I'm

NHibernate - CreateCriteria vs CreateAlias

岁酱吖の 提交于 2019-11-27 06:45:24
Assuming the following scenario: class Project{ public Job Job; } class Job{ public Name; } Assuming I want to use the Criteria API to search for all projects whose Job has the name "sumthing". I could use the CreateAlias to create an alias for Job and use it to access Name, or I could create a new Criteria for the property Job and search by Name. Performance wise, is there any difference? Jaguar given these requirements there would be no difference, the generated SQL is the same: for mappings: <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

How to query flags stored as enum in NHibernate

坚强是说给别人听的谎言 提交于 2019-11-27 06:25:27
问题 How to do either a HQL or a Criteria search (the latter is preferred) involving an enum that is used as flags. In other words, I have a persisted enum property that stores some kind of flags. I want to query all the records that have one of these flags set. Using Eq won't work of course because that will only be true, if that is the only flag set. Solving this using the Criteria API would be the best, but if this is only doable using HQL that is good too. 回答1: Here's how you could do it with

Using groupProperty and countDistinct in Grails Criteria

喜欢而已 提交于 2019-11-27 06:15:59
问题 I'm using Grails 1.2.4. I would like to know on how can I sort by "countDistinct" (descending) and with groupProperty inside a projections. Here are my domains: class Transaction { static belongsTo = [ customer : Customer, product : Product ] Date transactionDate = new Date() static constraints = { transactionDate(blank:false) } } class Product { String productCode static constraints = { productCode(blank:false) } } In MySQL terms, this is what I want: select product_id, count(product_id)

Hibernate Criteria API - HAVING clause work arounds

醉酒当歌 提交于 2019-11-27 06:04:40
问题 I've written a query using Hibernate Criteria API to grab a summation of a particular value, now I need to be able to restrict the result to rows where that sum is greater or equal to a particular value. Normally I would use a HAVING clause in my SQL to do this, but the Criteria API doesn't seem to support that at this moment. In raw SQL this is what I need it to do: SELECT user_pk, sum(amount) as amountSum FROM transaction GROUP BY user_pk HAVING amountSum >=50; One work-around that I

Hibernate Query By Example and Projections

帅比萌擦擦* 提交于 2019-11-27 05:03:48
问题 To make it short: hibernate doesn't support projections and query by example? I found this post: The code is this: User usr = new User(); usr.setCity = 'TEST'; getCurrentSession().createCriteria(User.class) .setProjection( Projections.distinct( Projections.projectionList() .add( Projections.property("name"), "name") .add( Projections.property("city"), "city"))) .add( Example.create(usr)) Like the other poster said, The generated sql keeps having a where class refering to just y0_= ? instead