hql

Using Left Joins in HQL on 3 Tables

我的未来我决定 提交于 2019-12-31 21:42:07
问题 I have three tables A B and C. Now i want to execute this sql query in HQL: select * from A as a left join B as b on a.id = b.id left join C as c on b.type=c.type; Need help in writing equivalent HQL. I tried with this HQL... Query q = session.createQuery( "FROM A as a LEFT JOIN B as b on a.id=b.id LEFT JOIN C as c on b.type=c.type"); This query is throwing exception ..... org.hibernate.hql.ast.QuerySyntaxError: unexpected token: LEFT near line 1, column 23 [FROM com.admin.A as a LEFT JOIN B

Hibernate queries slow down drastically after an entity is loaded in the session

早过忘川 提交于 2019-12-31 10:44:52
问题 I'm using Hibernate EntityManager, and am experiencing a weird slowdown in my Hibernate queries. Take a look at this code: public void testQuerySpeed() { for(int i = 0; i < 1000; i++) { em.createNativeQuery("SELECT 1").getResultList(); } } This runs in about 750ms on my machine. Not blazing fast considering it's just selecting a constant integer, but acceptable. My problem arises the moment ANY entities are loaded in my EntityManager session before I launch my query: public void

Hibernate queries slow down drastically after an entity is loaded in the session

一世执手 提交于 2019-12-31 10:44:08
问题 I'm using Hibernate EntityManager, and am experiencing a weird slowdown in my Hibernate queries. Take a look at this code: public void testQuerySpeed() { for(int i = 0; i < 1000; i++) { em.createNativeQuery("SELECT 1").getResultList(); } } This runs in about 750ms on my machine. Not blazing fast considering it's just selecting a constant integer, but acceptable. My problem arises the moment ANY entities are loaded in my EntityManager session before I launch my query: public void

用Hibernate框架把hql生成可执行的sql语句-Oracle方言

纵然是瞬间 提交于 2019-12-31 07:49:18
  引言:为什么这样的需求,源自公司项目需要,公司的项目使用java的struts2+spring2.5+oracle中间件tuxedo,数据库用的是Oracle,但由于不直接连接数据库,用中间件处理的方式,又不希望有太多服务,所以就开始网络找资料整理编码了。大概花了一个多星期完成了这个任务,现在整理出来与大家分享,也是自己知识的梳理。    1. 需要导入相关的 jar 包 : [ 按字母顺序排列 ] antlr-2.7.5H3.jar           语言转换工,Hibernate利用它实现 HQL 到 SQL的转换 asm.jar               ASM 字节转换库 cglib-2.1.2.jar            高效的代码生成工具, Hibernate用它在运行时扩展 Java类和实现 Java 接口 classes12.jar             Oracle数据库驱动 commons-collections-2.1.1.jar    Apache 的工具集,用来增强Java对集合的处理能力 commons-logging-1.0.4.jar     Apache 软件基金组所提供的日志工具 dom4j-1.6.1.jar          dom4j XML 解析器 hibernate.jar           Hibernate的核心库

Hibernate中HQL语句写法

十年热恋 提交于 2019-12-31 07:49:01
最开始项目中HQL语句写的比较随意,后来看了下Hibernate 中Query和 Criteria API,和sql语句的编译过程 。了解到查询条件不要直接使用字符串拼接,可以大大提高sql语句执行效率,代码也更加规范安全。 1,hql中可以用"?"来占位,在使用query时,必须将"?"及其索引位置和字段名对应上(很容易出错;不利于维护;) 2,hql另外一种方式占位:命名参数; 使用命名参数;冒号+字段名(或别名);等于给原来的"?"起了个名字; String hql = "from User as user where user.name=:name"; //.... query.setString ("name", name); // 此处第一个参数必须和hql中参数命名一致; query 的基本写法:   使用“=:xx” 标识变量   setString("xx", ....) 插入对应的搜索条件 public String DocSerialNo(String perType, String organId) { String updateHql = "update LicenceSerialNo set curnum=curnum+1 where curareaid=:organId and areaid=:perType"; Query qr=this

HQL joins in Grails: Part Deux

这一生的挚爱 提交于 2019-12-31 03:48:28
问题 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

Query using “CASE WHEN” statement in WHERE causes QuerySyntaxException: unexpected AST

纵饮孤独 提交于 2019-12-31 01:52:06
问题 I'm trying to make a query using Spring Data, but I cannot make it work: @Query(SELECT t FROM Thing t WHERE name LIKE :name AND CASE WHEN (:minVal <= 0) THEN TRUE ELSE (val <= :minVal) END AND CASE WHEN (:maxVal <= 0) THEN TRUE ELSE (val >= :maxVal) END) Page<Thing> getThings(@Param("name") String name, @Param("maxVal") int maxVal, @Param("minVal") minVal); StackTrace: Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: CASE

How to put 'null' into column using HQL?

穿精又带淫゛_ 提交于 2019-12-30 18:29:50
问题 How to build valid HQL string, which is equivalent to UPDATE table SET field = null WHERE .... 回答1: Do you mean bulk HQL update? Try this UPDATE myEntity e SET e.myProperty = null WHERE ... You can also use a parameterized version of the above UPDATE myEntity e SET e.myProperty = :param WHERE ... In your code: int updatedEntities = session.createQuery(updateQueryHQL) .setString( "param", myValue ) // or .setString( "param", null ) .executeUpdate(); See documentation for details. If you're not

Filling in NULLS with previous records - Netezza SQL

可紊 提交于 2019-12-30 10:11:43
问题 I am using Netezza SQL on Aginity Workbench and have the following data: id DATE1 DATE2 1 2013-07-27 NULL 2 NULL NULL 3 NULL 2013-08-02 4 2013-09-10 2013-09-23 5 2013-12-11 NULL 6 NULL 2013-12-19 I need to fill in all the NULL values in DATE1 with preceding values in the DATE1 field that are filled in. With DATE2, I need to do the same, but in reverse order. So my desired output would be the following: id DATE1 DATE2 1 2013-07-27 2013-08-02 2 2013-07-27 2013-08-02 3 2013-07-27 2013-08-02 4

SQLGrammarException: could not execute query

元气小坏坏 提交于 2019-12-30 09:33:13
问题 I am using Struts2 & Hibernate and getting below error when I searched data with string test but works for me when I searched with numeric 111 . I am getting this value from bean class and defined property of string type in bean class. Below I am providing code: public String retrieveRecords() { String empId = p.getEmpId(); String paramValue = ""; if(empId !=null) if(!(empId.isEmpty())) paramValue =" where b.empId="+empId; String empName = p.getEmployeeName(); if(empName !=null && empName !=