criteria

Joining on multiple fields in a NHibernate Criteria query

試著忘記壹切 提交于 2019-12-02 04:17:13
I have a Dept table and a Emp table. I need to join these two table in such a way that the where clause looks something like this: where dept.deptId = emp.DeptId and dept.deptName = emp.empTrainingName I tried this: Criteria criteria = session.createCriteria(Dept.class).createAlias("empMap","id"); Using this, the first where condition i.e. dept.deptId = emp.DeptId is performed. But I am not sure how to compare dept.deptName with emp.empTrainingName . How do I do this using the Criteria API in NHibernate? Criteria criteria = session.createCriteria(Dept.class, "department") .createAlias("empMap"

Hibernate — Criteria vs named query

僤鯓⒐⒋嵵緔 提交于 2019-12-02 03:59:09
问题 I'm trying to compare Hibernate Criteria to named queries for performance. i'm aware it all depends on the actual query itself and the last word is on how they profile in runtime. still, trying to sort out what goes into each. i tried to organize the Q in two parts & looking for verification/correction on both: PART-1 -- how Hibernate Criteria and named queries work basically: Criteria works on parameters. In runtime, the query doesn`t need parsing-- has several search and "present-in-form"

Regular expression with criteria

做~自己de王妃 提交于 2019-12-02 03:24:33
I have a table in which certain words or word groups are stored. I want to select entries which start with an uppercase letter, cointain no space and contain only letters. My SQL looks like this: select word from words where w_id > 100 AND word REGEXP '^[A-Z][A-Za-z]*$' limit 2000; How do I do the same thing using criteria? user3487063 Try this: List words = session.createCriteria(Word.class) .setProjection(Projections.property("word")) .add(Restrictions.and(Restrictions.gt("w_id",100), Restrictions.sqlRestriction(word REGEXP '^[A-Z][A-Za-z]*$'))) .setMaxResults(2000).list(); 来源: https:/

NHibernate Criteria Query - Select Distinct with Joined Entity

纵然是瞬间 提交于 2019-12-02 03:06:32
问题 I have a Person entity. Every person has a country, I want to select all the distinct countries that have people in them. This Criteria Query returns all the distinct CountryID's criteria.SetProjection(Projections.Distinct(Projections.Property("Country"))); How do I alter it to join and fetch the Country entity, not just the ID? 回答1: Any easy way would be to use a subquery. That is, you could select the whole country on the outer query where the country ID matches the inner query. Subqueries

NHibernate Criteria: Subquery After 'From' Clause

杀马特。学长 韩版系。学妹 提交于 2019-12-02 02:45:18
问题 How can I write the following SQL using Criteria: select b.Name as Batch, b.Capacity as Capecity, a.tStudent as Admit, (b.Capacity-a.tStudent) as Availabe from ( SELECT count(Id) as tStudent, BatchId FROM [dbo].[Student] group by BatchId) as a left join [dbo].[Batch] as b on a.BatchId = b.Id 回答1: To use NHibernate, to produce query like this: SELECT ... FROM ( SELECT ... ) AS a .. We have to options: Map the sub-select as an Entity. create raw SQL query The first option would mean to create

NHibernate Criteria Query - Select Distinct with Joined Entity

故事扮演 提交于 2019-12-02 02:34:54
I have a Person entity. Every person has a country, I want to select all the distinct countries that have people in them. This Criteria Query returns all the distinct CountryID's criteria.SetProjection(Projections.Distinct(Projections.Property("Country"))); How do I alter it to join and fetch the Country entity, not just the ID? Any easy way would be to use a subquery. That is, you could select the whole country on the outer query where the country ID matches the inner query. Subqueries.PropertyIn( "Country", innerDetachedCriteriaWhichFindsCountriesWithPeopleAndProjectsCountryId) 来源: https:/

NHibernate Criteria: Subquery After 'From' Clause

与世无争的帅哥 提交于 2019-12-02 01:19:56
How can I write the following SQL using Criteria: select b.Name as Batch, b.Capacity as Capecity, a.tStudent as Admit, (b.Capacity-a.tStudent) as Availabe from ( SELECT count(Id) as tStudent, BatchId FROM [dbo].[Student] group by BatchId) as a left join [dbo].[Batch] as b on a.BatchId = b.Id To use NHibernate, to produce query like this: SELECT ... FROM ( SELECT ... ) AS a .. We have to options: Map the sub-select as an Entity. create raw SQL query The first option would mean to create some view , and map it as an Entity. If we do not like view (or cannot create it) we can use the power of

Convert date to string in Hibernate Criteria

耗尽温柔 提交于 2019-12-02 00:01:03
Is it possible to perform a request that checks the string representation of a Date instance. For example Restrictions.like("dateField", "%12%") to retrieve dates that either have String 12 in day or 12 in month or 12 in year where "dateField" is an instance of java.util.Date Thanks I had the same problem and here's what I did: First, I created my own Criterion implementation: public class DateLikeExpression implements Criterion { private static final long serialVersionUID = 1L; private String propertyName; private String value; public DateLikeExpression(String propertyName, String value) {

hibernate criteria使用总结

▼魔方 西西 提交于 2019-12-01 23:28:05
由于以前没有接触过Hibernate而且近期又经常要用到,所以先在次记录下来以便以后的不时之需。 Hibernate支持符合java编写习惯的查询API,先通过Session简历一个 Hibernate 支持一种符合Java撰写习惯的查询API,使用Session建立一个Criteria对象,然后在不使用Sql甚至Hql的情况下对数据库进行查询。 以最基本的查询来说,如果您想要查询某个物件所对应的资料表中所有的内容,可以直接使用: Criteria criteria = session.createCriteria(T.class); List T= criteria.list(); 其中最经常用 Restrictions 的静态方法传回 criteria 实例 传回的每个Criteria实例代表着一个条件,另外还要使用 Criteria 的add方法加入这些条件实例。 Restrictions的几个常用限定查询方法如下表所示: 方法 说明 Restrictions.eq 等于 Restrictions.allEq 使用Map,使用key/value进行多个等于的比对 Restrictions.gt 大于 > Restrictions.ge 大于等于 >= Restrictions.lt 小于 < Restrictions.le 小于等于 <= Restrictions

Hibernate Query数据查询

感情迁移 提交于 2019-12-01 20:02:17
主要由三种查询:HQL查询、Criteria条件查询、SQL查询。 以下分别讲解 1. HQL查询 HQL(Hibernate Query Language)查询提供了更加丰富和灵活的查询特性,因此Hibernate将HQL查询立为官方推荐的标准查询方式。 HQL语法与SQL相似,但HQL是一种面向对象的查询语言,操作的是类、实例和属性等。 HQL是完全面向对象的查询语言,支持继承和多态等特性。 HQL查询步骤 (1) 获得Hibernate Session对象 (2) 设计HQL查询语句 (3) 以HQL查询语句作为参数,调用Session.createQuery(hql)方法创建查询对象 (4) 如果HQL查询语句包含参数,调用Query的setXxxx()方法为参数赋值 (5) 调用Query对象的list等方法遍历查询结果 public class HqlQueryTest{ private void findStudents(){ SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction trans = session