hql

Hibernate detached queries as a part of the criteria query

故事扮演 提交于 2019-12-02 18:37:58
java experts can you please help me write detached queries as a part of the criteria query for the following SQL statement. select A.* FROM AETABLE A where not exists ( select entryid FROM AETABLE B where B.classpk = A.classpk and B.userid = A.userid and B.modifiedDate > A.modifiedDate ) and userid = 10146 You need to write a correlated subquery . Assuming property / class names match column / table names above: DetachedCriteria subquery = DetachedCriteria.forClass(AETable.class, "b") .add(Property.forName("b.classpk").eqProperty("a.classpk")) .add(Property.forName("b.userid").eqProperty("a

hql手动创建表

拟墨画扇 提交于 2019-12-02 15:31:29
---------------------------------手动用代码进行数据库创建------------------------------ Configuration con=new Configuration(); con.configuration(); //创建工具类对象 SchemaExport export=new SchemaExport(con); //建表 //第一个参数 控制台打印建表语句 //第二个参数 执行语句 export.create(true,true); ----------------------------------hibernate.cfg.xml方式创建数据库表-------------------------------- 服务启动的时候就会创建 <property name=" hibernate.hbm2ddl.auto "> create-drop </property> 每次在创建 sessionFactory 时候执行创建表; 当调用 sesisonFactory 的 close 方法的时候,删除表! <property name=" hibernate.hbm2ddl.auto "> create </property> 每次都重新建表,如果表已经存在就先删除再创建 <property name="

NHibernate HQL Inner Join (SQL Server,Visual C#)

谁说胖子不能爱 提交于 2019-12-02 15:19:31
问题 I want to using HQL with inner Join. But, a query syntax exception is thrown. This is my C# code: string sqlQuery = "Select fq FROM Answers as fq INNER JOIN Questions as q " + " on fq.questionId=q.questionId"; IList Result; int count = 0; try { using (ISession session = ConnectionModule.OpenSession()) { IQuery query = session.CreateQuery(sqlQuery); session.CreateCriteria(typeof(Answers)); Result = query.List(); } } catch(Exception ex) { MessageBox.Show(ex.Message+"\n"+ex.InnerException); }

How to pass parameter to @Query annotation with hql

て烟熏妆下的殇ゞ 提交于 2019-12-02 13:17:57
Here is my hql requete : @Query("select a from Agent where a.visibility = true a order by a.id desc") public Page<Agent> getAllAgents(Pageable pageable); I want to select all agents that have visibility true. In my Agent class a have Boolean visibility attribute with getVisibility and setVisibility functions. In my data base "visibility" stored as bit(1). I tried a.visibility = 1, ... = '1', ...= 'TRUE', ...='true', ... is true. But i get this error : Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: a near line 1, column 74 [select a from com.GemCrmTickets

JPA/Hibernate: Escaping HQL keywords

醉酒当歌 提交于 2019-12-02 12:22:12
This is similar to How to escape reserved words in Hibernate's HQL . But I use JPA, so the solution isn't applicable. So, how can I escape HQL keywords in JPA? Example query: ( count is the offending.) em.createQuery("INSERT INTO Count (id, count) SELECT 1, ?").setParameter(1, id).executeUpdate(); There's this jira HHH-3811 , still open. Not sure if relevant since it's about SQL keywords, not HQL keywords. You could try something like this: em.createQuery("INSERT INTO Count c (c.id, c.count) SELECT 1, ?").setParameter(1, id).executeUpdate(); That should make it clear to JPA that you mean

How to execute normal SQL query in Hibernate without HQL?

只愿长相守 提交于 2019-12-02 10:17:29
I have a pretty complex Join query to select few items from DB, and it doesn't involve any Update required back to this table. Which is why, I don't want to use the HQL (Hibernate Query Language, instead I want to execute as a simple SQL query. Is that possible to execute a Normal SQL - Join query which involves 3 different tables in hibernate? I use Java - Struts framework. If you say it is not possible then I have to stick to HQL and I would post here the query for which I would need your help in writing down HQL based class(tables) creations and HQL based Query string. Also, if you could

Hibernate Dynamic Order

♀尐吖头ヾ 提交于 2019-12-02 08:03:29
Hi i want to sort in HQL ORDER BY IF g.groupAdminId=:adminid THEN 1 ELSE 0 END ASC But it doesn't work, i want to have all entities where the user is admin first, how can i archieve this? Luka Klepec I don't believe it is possible to put named parameters outside a where clause. It is possible to order according to expressions: from User U order by case when U.group.name = 'Admin' then 0 when U.group.name = 'Superuser' then 1 else 2 end asc More on case in HQL docs : For your particular problem (having admins before other users) I suggest making two queries and combining the two lists in Java.

HQL joins in Grails: Part Deux

拜拜、爱过 提交于 2019-12-02 07:53:34
This is an extension of the question I asked here I have a relationship like this class Foo { static hasMany = [bars: Bar] } class Bar { // Has nothing to tie it back to Foo or Thing } class Thing { static hasMany = [bars: Bar] } I have an instance of Thing . I want to get all instances of Foo that are associated with all instances of Bar that are associated with the instance of Thing that I have. Is what I want possible via HQL (is HQL somehow aware of the indirect relationship between Thing and Foo )? UPDATE: Here is a picture of a possible relationship. If I had Thing1 and I wanted all the

NHibernate HQL Inner Join (SQL Server,Visual C#)

假如想象 提交于 2019-12-02 07:19:27
I want to using HQL with inner Join. But, a query syntax exception is thrown. This is my C# code: string sqlQuery = "Select fq FROM Answers as fq INNER JOIN Questions as q " + " on fq.questionId=q.questionId"; IList Result; int count = 0; try { using (ISession session = ConnectionModule.OpenSession()) { IQuery query = session.CreateQuery(sqlQuery); session.CreateCriteria(typeof(Answers)); Result = query.List(); } } catch(Exception ex) { MessageBox.Show(ex.Message+"\n"+ex.InnerException); } The point here is CROSS JOIN if there are no mapped relations, JOIN on existing (mapped) relations. So,

Do I have to refresh entities after bulk updates / deletes with HQL?

旧城冷巷雨未停 提交于 2019-12-02 07:05:14
I have written some DAO methods that do bulk updates / deletes with HQL but I see that when the query is executed the entities in memory are not sychronized (the cache is not updated). Say, I have a collection of Projects with a collection of Groups each and I want to delete all Groups. I can iterate the Groups and delete each but I prefer to run a bulk delete with HQL and IN operator. However, the list has the old objects after the query is executed. I realized that I have to refresh the objects with session.refresh(). Is there any other way I can bulk update and update cache automatically?