hql

How can I use Datediff() in HQL

半世苍凉 提交于 2019-12-02 04:07:57
I'm trying to get the difference between dates. In my SQL SERVER it's works fine: Select CSERVICE_ORDER_ID FROM TSERVICE_ORDERS WHERE DATEDIFF(DAY, CDB_CREATE_DATE_TIME, CCANCELLATION_DATE) = 4 But in my HQL query I'm not getting it. The function Datefiff works in HQL Query? Is there any function with the same behavior? HQL doesn't support datediff, but if you still want to use datediff, you should use createNativeQuery() or createSQLQuery() to write that in sql. In your example, you just need the id anyway, not entity object, so this should be enough. or you can use in hql something like this

Unicode String in Hibernate Queries

喜你入骨 提交于 2019-12-02 03:34:24
问题 In SQL one can write a query that searches for a name of a person like this: SELECT * FROM Person P WHERE P.Name LIKE N'%ike%' This query would run with unicode characters (assuming that the Name column and database were setup to handle unicode support). I have a similar query in HQL that is run by Hibernate (NHibernate). The generated query looks like: SELECT P FROM SumTotal.TP.Models.Party.Person P join P.Demographics PD WHERE (PD.LastName LIKE '%カタカ%' ) Unfortunately, placing a 'N' in

Hibernate hql/criteria result contains collection

左心房为你撑大大i 提交于 2019-12-02 02:44:13
I have two entities: public class Photo { Long id; String url; @ManyToOne @JoinColumn(name ="user_id") User user; // other fields and getters/setters } And second: public class User { Long id; @OneToMany(mappedBy = "user") private Collection<Photo> photos; // other fields and getters/setters } I am trying to get this DTO: public class UserDTO { Long id; List<String> photosUrls; } But I can't find the right solution. I wrote next criteria - find user with photos by login: getCurrentSession().createCriteria(User.class, "user") .createAlias("user.photos", "photos") .setProjection

Why does setParameter not set the parameter?

感情迁移 提交于 2019-12-02 02:12:42
问题 I'm using the following code to (attempt to) query a database: Query query = session.createQuery("from Trace where service = :service"); query.setParameter("service", clientRequest[0]); Where clientRequest[0] is from an array of Strings and the service variable is a String in my POJO mapped to a VARCHAR(45) in my MySQL database. When I run this code, Hibernate shows the executed SQL query as: Hibernate: select trace0_.traceId as traceId0_, trace0_.service as service0_, trace0_.longitude as

HQL with fully qualified class name

不打扰是莪最后的温柔 提交于 2019-12-02 02:05:59
问题 Let's say I have entity Foo like - package com.some.company.model; // imports @Entity public class Foo{ @Id private Long id; // getters / setters and other properties omitted } so while dealing with Entity through HQL I prefer to refer the Entity by fully qualified class name like - entityManager.createQuery(String.format("delete from %s where id = :id", Foo.class.getName())) .setParameter("id", fooId) .executeUpdate(); I noticed one thing in @Entity annotation - the name property has a

How to pass parameter in HQL query

别来无恙 提交于 2019-12-02 01:40:58
find below my HQL query Query query = session.createQuery("select u from UserLog u where u.userLogSerialno = " + "(select max(uu.userLogSerialno) from UserLog uu where uu.userId = u.userId)"); This query is working fine but in this, I want to pass the value of userId but I am not able to figure out how to do this. Kindly Help..!! Thanks in Advance..!! I is very simple to add parameter to an HQL Query query = session.createQuery("select u from UserLog u where u.userLogSerialno = " + "(select max(uu.userLogSerialno) from UserLog uu where uu.userId = :userId)").setParameter("userId", 15); here i

Why does setParameter not set the parameter?

自古美人都是妖i 提交于 2019-12-02 01:33:32
I'm using the following code to (attempt to) query a database: Query query = session.createQuery("from Trace where service = :service"); query.setParameter("service", clientRequest[0]); Where clientRequest[0] is from an array of Strings and the service variable is a String in my POJO mapped to a VARCHAR(45) in my MySQL database. When I run this code, Hibernate shows the executed SQL query as: Hibernate: select trace0_.traceId as traceId0_, trace0_.service as service0_, trace0_.longitude as longitude0_, trace0_.latitude as latitude0_, trace0_.timestamp as timestamp0_ from trace trace0_ where

HQL Query to check whether table has data or not

人走茶凉 提交于 2019-12-02 01:30:21
Initially database created it doesn't have any table. I write code to update values using parameterised query but value doesn't exist initially then how we can handle it? You can try like this: public Boolean existsOrNot (DTOAny i) { Query q = getSession(). createQuery("select 1 from DTOAny t where t.key = :key"); q.setString("key", i.getKey() ); return (q.uniqueResult() != null); } ( Assuming that the table exists and you are checking that there is data in the table or not ) 来源: https://stackoverflow.com/questions/29745464/hql-query-to-check-whether-table-has-data-or-not

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

泄露秘密 提交于 2019-12-02 01:01:20
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 near line 1, column 49 [SELECT t FROM Thing t WHERE name LIKE :name AND CASE WHEN (:minVal <= 0) THEN

how to use string left function in hql

China☆狼群 提交于 2019-12-01 22:41:45
I have a sql query like this select column from table where path = left('INPUTSTRING', length(path)); and trying to accomplish it in hql like this, return session.createQuery("from Table where Path = left(:input, length(Path))"). query.setParameter("input", inputPath). .list(); and getting an error like this Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: left near line 1 how to get this done? What is the corresponding string function in hql? Is there a solution for this using criteria query apis? Yes, left() is not supported by the MySQLDialect . See the list of HQL