hql

Write HQL clause using Hibernate Criteria API

旧时模样 提交于 2019-12-21 05:05:06
问题 I want to write a method that returns a list of last added objects grouped by field 'serviceId'. The following HQL works, but I want to write this using Criteria API: FROM Notification WHERE date IN (SELECT MAX(date) FROM Notification GROUP BY serviceId) ORDER BY date ASC Something like this: Criteria crit = session.createCriteria(Notification.class); crit.add(Restrictions.in("date", <MAX dates>)); criteria.addOrder(Order.desc("date")); Thanks in advance. EDIT: Now I need a similar query that

hibernate - HQL joins on many clauses

落爺英雄遲暮 提交于 2019-12-21 04:18:10
问题 I've been reading Hibernate documentation, but I haven't found anything that would explain how to do the following. I have the following SQL code that I'm trying to convert to HQL: SELECT {msg.*}, {cmd.*} FROM Schema.Messages AS msg LEFT OUTER JOIN schema.send_commands AS cmd ON cmd.message_key = msg.unique_key AND ( lower(cmd.status) IN (lower('failed') ) ) WHERE msg.sequence_received < 10"; The mainissue I'm having is that I'm unable to have two clauses on a LEFT OUTER JOIN. HQL allows me

How do I use the current date in an HQL query with an Oracle database?

无人久伴 提交于 2019-12-21 03:08:16
问题 I'm trying to write this query using Hibernate 3 and Oracle 10. from Alert alert where alert.expiration > current_date() order by alert.priority, alert.updated, alert.name It's creating SQL like this - Hibernate: select alert0_.ANNOUNCEMENTS_ID as ANNOUNCE1_1_, alert0_.ANNOUNCEMENT S_NAME as ANNOUNCE2_1_, alert0_.ANNOUNCEMENTS_PRIORITY as ANNOUNCE3_1_, alert0_. ANNOUNCEMENTS_EXPIRATION as ANNOUNCE4_1_, alert0_.ANNOUNCEMENTS_UPDATE_DATE as A NNOUNCE5_1_ from NYC311_ANNOUNCEMENTS alert0_ where

IFNULL equivalent in Hibernate Query Language?

霸气de小男生 提交于 2019-12-20 18:15:13
问题 I'm trying to write an HQL query which will calculate an average rating for an item. I want the query to return 0 instead of null when there are no rating for a given item - so that I can use my query as a subquery. So is it possible? Is there an HQL equivalent of IFNULL or NVL? 回答1: COALESCE is the official equivalent. It returns the first non-null of its arguments. Example: COALESCE(id_pati, 0) Link Wikipedia 回答2: Nhibernate docs are out of date. Check http://docs.jboss.org/hibernate/stable

How to dynamically order many-to-many relationship with JPA or HQL?

被刻印的时光 ゝ 提交于 2019-12-20 14:15:36
问题 I have a mapping like this: @ManyToMany(cascade = CascadeType.PERSIST) @JoinTable( name="product_product_catalog", joinColumns={@JoinColumn(name="product_catalog", referencedColumnName="product_catalog")}, inverseJoinColumns={@JoinColumn(name="product", referencedColumnName="product")}) public List<Product> products = new ArrayList<Product>(); I can fetch the products for the catalog nicely, but I can't (dynamically) order the products. How could I order them? I probably have to write a many

Querying with NHibernate

好久不见. 提交于 2019-12-20 10:46:56
问题 I am new to NHibernate and I am trying to learn how to query my data. Below is the configuration xml. Only the recipe is shown. I want to be able to query recipes by recipetitle from keywords entered and also ingredients from ingredientname. So you might enter "pasta wine" for example. This is what I have tried but gives me an error. hql = "from Recipe r " + "left join r.Images " + "inner join r.User " + "inner join r.Ingredients i " + "where i.IngredientName Like '%pasta%' OR i

Prefered way to map a result set with entity and count using Spring Data

一笑奈何 提交于 2019-12-20 07:24:47
问题 There is often the business question to show all categories and how often these categories are used. This question is easy to answer with an query: SELECT c.*, count(*) FROM category_assignment ca LEFT JOIN category c on ca.c_id = c.id group by c.id What i am asking for is your suggested way to map the result set based on the following: @Entity public class CategoryAssignment { @Id int id; @ManyToOne(fetch = FetchType.EAGER) private Category category; @ManyToOne(fetch = FetchType.EAGER)

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

自闭症网瘾萝莉.ら 提交于 2019-12-20 04:54:03
问题 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

How can I use Datediff() in HQL

99封情书 提交于 2019-12-20 04:24:38
问题 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? 回答1: 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

HQL Query to check whether table has data or not

匆匆过客 提交于 2019-12-20 03:11:12
问题 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? 回答1: 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:/