hql

How to test HQL queries?

柔情痞子 提交于 2019-12-17 22:34:04
问题 I'm searching for a fast (really fast) way to test changes to hibernate queries. I have a huge application with thousands of different HQL queries (in XML files) and 100+ mapped classes and i dont want to redeploy the whole application to just test one tiny change to a query. How would a good setup look like to free me from redeployment and enable a fast query check? 回答1: With Intellij IDEA 8.1.3 the mechnism of choice is called 'Facet'. To instantly test HQL queries: create a data source

how to use Oracle's regexp_like in Hibernate HQL?

℡╲_俬逩灬. 提交于 2019-12-17 19:39:27
问题 I am using oracle 10g and hibernate 3.3.2 . I have used regular expression in sql before, now for the first time I am using it in HQL. Query query = getSession().createQuery("From Company company where company.id!=:companyId and regexp_like(upper(rtrim(ltrim(company.num))), '^0*514619915$' )"); This is my hql, when i run it without regex_like function it runs as expected. But I am not able to execute it with regex_like expression. It says.. nested exception is org.hibernate.hql.ast

How to perform date operations in hibernate

别来无恙 提交于 2019-12-17 19:13:16
问题 I want to perform data time operations using hibernate HQL. I want to add and subtract two dates as well as I want to subtract 1 year or 1 month from a particular date. How is this possible using HQL in hibernate? 回答1: See Performing Date/Time Math In HQL? for an example. To use custom sql you must wrote an own hibernate dialect and register: registerFunction("weekday", new SQLFunctionTemplate(Hibernate.INTEGER, "to_char(?1,'D')") ); 回答2: You need to create your own dialect. Something like

Join without association in HQL

我们两清 提交于 2019-12-17 17:48:07
问题 Lets say I have two tables(A, B) like: A {id, a, c} B {id, b, c} I have also their entities. I want to write an HQL so that the result set will be like (where A.c = B.c): (a1, b1, c1) (a2, b2, c2) (a3, b3, c3) ... Since on clauses are not supported by hibernate I am stuck and I don't know how to write the query. 回答1: You have to use the cross join notation: from A as table_a , B as table_b where table_a.c = table_b.c Of course there is no way to implement outer joins in this manner, so you

How to do a Union SQL statement in HQL?

耗尽温柔 提交于 2019-12-17 16:44:19
问题 I'm trying to create a Union between two tables, using HQL (Hibernate Query Language). This SQL script works fine on my SQL server: SELECT COUNT(DISTINCT linkedin_id) as test, school_name FROM (SELECT * FROM alum_education UNION SELECT * FROM alum_connection_educations) AS UNIONS where school_name='some string' the problem is, when i try to run it in grails like this: def countOfEdu = AlumEducation.executeQuery("select count (distinct linkedinId ) as countOfEdu, schoolName as SchoolName from

How do I write hql query with cast?

三世轮回 提交于 2019-12-17 16:43:07
问题 I need to combine 2 tables using hql, both are having common column, but table1 common column is integer and table2 common column is String For example, select a.id as id,a.name as name,b.address as address from Personal as a,Home as b where a.id=b.studid Here a.id is an integer while b.stduid is a string , but Data of both columns is the same. How can I get the result of the query using hql query? 回答1: HQL supports CAST (if underlying database supports it), you can use it: select a.id as id

How to simulate NVL in HQL

倾然丶 夕夏残阳落幕 提交于 2019-12-17 09:37:58
问题 I tried this: from Table where (:par1 is null or col1 = :par1) but it happens that from Table where :par1 is null always returns all the rows of the table, even if the :par1 is not null. while select * from table where col1 = 'asdf' does not return any row. I can't use native grammars because my application is supposed to run on different database engines 回答1: The equivalent to the nvl command in HQL is the coalesce command. coalesce(a,b) will return a if a is not null, otherwise b . So you

Hibernate HQL Query : How to set a Collection as a named parameter of a Query?

我们两清 提交于 2019-12-17 08:54:57
问题 Given the following HQL Query: FROM Foo WHERE Id = :id AND Bar IN (:barList) I set :id using the Query object's setInteger() method. I would like to set :barList using a List of objects, but looking at the Hibernate documentation and list of methods I cannot see an obvious choice of which to use. Any ideas? 回答1: Use Query.setParameterList() , Javadoc here. There are four variants to pick from. 回答2: I'm not sure about HQL, but in JPA you just call the query's setParameter with the parameter

Hibernate中Hql查询

半腔热情 提交于 2019-12-17 07:05:30
这篇随笔将会记录hql的常用的查询语句,为日后查看提供便利。 在这里通过定义了三个类,Special、Classroom、Student来做测试,Special与Classroom是一对多,Classroom与Student是一对多的关系,这里仅仅贴出这三个bean的属性代码: Special类: public class Special { private int id; private String name; private String type; private Set<Classroom> rooms; .......... } Classroom类: public class Classroom { private int id; private String name; private Special special; private Set<Student> students;   ............ } Student类: public class Student { private int id; private String name; private String sex; private Classroom room; .......... } 1.最简单的查询 List<Special> specials = (List<Special>

NHibernate How do I query against an IList<string> property?

坚强是说给别人听的谎言 提交于 2019-12-17 06:50:27
问题 I am trying to query against an IList<string> property on one of my domain classes using NHibernate. Here is a simple example to demonstrate: public class Demo { public Demo() { this.Tags = new List<string>(); } public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual IList<string> Tags { get; set; } } Mapped like this: <class name="Demo"> <id name="Id" /> <property name="Name" /> <bag name="Tags"> <key column="DemoId"/> <element column="Tag" type="String" /