hql

How to avoid unnecessary selects and joins in HQL and Criteria

无人久伴 提交于 2019-12-06 01:44:42
问题 I have been trying different combinations of HQL and Criteria and I haven't been able to avoid some unnecessary joins (in both) and some unnecessary selects (in Criteria). In our scenario, we have a @ManyToMany relationship between Segment and Application entities (navigation is from Segment to Applications). First I tried this Criteria : Application app = ... List<Segment> segments = session.createCriteria(Segment.class) .createCriteria(Segment.APPLICATIONS) .add(Restrictions.idEq(app.getId(

Collection.contains(Enum.Value) in HQL?

怎甘沉沦 提交于 2019-12-06 01:27:19
问题 I'm a little confused about how to do something in HQL. So let's say I have a class Foo that I'm persisting in hibernate. It contains a set of enum values, like so: public class Foo { @CollectionOfElements private Set<Bar> barSet = new HashSet<Bar>(); //getters and setters here ... } and public enum Bar { A, B } Is there an HQL statement I can use to fetch only Foo instances who'se barSet containst Bar.B? List foos = session.createQuery("from Foo as foo " + "where foo.barSet.contains.Bar.B")

Hibernate, how to count with condition

寵の児 提交于 2019-12-06 00:07:50
I am using oracle and hibernate for mapping. I want to count with a condition in count() function. my code is: select count(case when st.averageMark < su.gradePass then 1 else 0 end) from Study st join st.subject su where st.acaYear in (2009) and st.semester = 4 and su.idSeq = 1330 group by st.acaYear the code return me nothing. I used sum instead of count it returned a result but it is wrong, the result is bigger than I suppose it to be. thank in advance. Nathanphan I just solved the problem with the following code. select sum(case when st.averageMark >= su.gradePass then 1 else 0 end) as

The data types datetime and time are incompatible in the greater than or equal to operator

青春壹個敷衍的年華 提交于 2019-12-05 23:04:50
I have a variable type time in a column of a table of the database. How can I compare this value in java with this field I mean can i use date, gregoriancalendar? I've tried adn I still have this message, please can someone give me an advice Date d2 = new Date(); // timestamp now Calendar cal = Calendar.getInstance(); // get calendar instance cal.setTime(d2); // set cal to date cal.set(Calendar.HOUR_OF_DAY, 10); // set hour to midnight cal.set(Calendar.MINUTE, 30); // set minute in hour cal.set(Calendar.SECOND, 0); // set second in minute cal.set(Calendar.MILLISECOND, 0); // set millis in

JPQL Selecting Record with Max Value

笑着哭i 提交于 2019-12-05 21:50:38
I'm trying to select the record that has the max value for a particular column and here's what I have so far: select o from Order o where o.orderNumber = :orderNumber and o.version = (select max(o.version) from Order o where o.orderNumber = :orderNumber) This seems to be generating the SQL: SELECT * FROM Order WHERE ordernumber = :orderNumber AND orderversion = (SELECT Max(orderversion) FROM order WHERE ordernumber = :orderNumber); I think the following SQL would be more efficient: SELECT * FROM order ord INNER JOIN (SELECT ordernumber, Max (version) AS version FROM order WHERE ordernumber =

HQL query for id pairs / tuples

我与影子孤独终老i 提交于 2019-12-05 19:45:51
I am trying to query for newly created relations between two domains using HQL. So I want to do something similar to this: select x.id, y.id from Author as x join x.books as y where (x.id, y.id) not in ((1,2),(3,4)) or select x.id, y.id from Author as x join x.books as y where (x.id, y.id) not in (:existingTuples) So existingTuples are the related IDs that I already know. And I would like to see, which relations have been created. I know it's possible to do it with SQL, but with HQL I would not have to care about, if it's 1-1, 1-n or n-m. If I am typing the IDs directly in the HQL i get this

Hibernate hql, execute multiple update statements in same query

牧云@^-^@ 提交于 2019-12-05 18:12:35
问题 I want to execute multiple update statements in the same query in hibernate Hql. like below: hql = " update Table1 set prob1=null where id=:id1; " + " delete from Table2 where id =:id2 "; ... query.executeUpdate(); in the same executeUpdate call I want to update records in Table1 and delete records from Table2. Is that possible? 回答1: In short, what you are looking is something like batching in JDBC. Thich is not provided by Hibernate for Bulk Update query, and I doubt if it will ever be

Nhibernate - How to debug “Antlr.Runtime.NoViableAltException”?

人盡茶涼 提交于 2019-12-05 17:42:24
With many HQL queries, time and time again I am getting this exception: Antlr.Runtime.NoViableAltException This is really generic and unhelpful - does anyone know how best to debug this? Obviously it's a problem with my HQL - but without any clue as to what exactly is wrong it's very much a case of trial and error. I'm pulling my hair out every time I see this. Note, I don't want to post any HQL here, becuase it's something that I am often coming across, not a problem related to one query. Does anyone know the best way to tackle this? Is there any tool for validating HQL queries? Have a look

HQL (Hibernate) how to check if a list of elements is a subset of another list?

偶尔善良 提交于 2019-12-05 16:58:37
I am having tough time creating a HQL query that checks if a list of records 'a' of table 'X' is included within another list 'b' of the same table 'X'. Here is an example i am using: select r from Role r where ( select p from Permission p where p.name = 'test') in elements(r.permissions) Now this query is just as an example, to clarify a bit, each role has 1 or more permission. Now here's what happen, if the the subselect (permission p) returns 1 row, all work fine. But if more are selected, the query fails, since I am trying to check if a list is within a list... I am sure I am missing

Why does using a column name directly in HQL only work sometimes?

こ雲淡風輕ζ 提交于 2019-12-05 12:30:33
I have two HQL queries I am using for a quick-and-dirty unit test. The first looks somewhat like this: from Foo where SOME_FOREIGN_KEY = 42 The second looks like this: from Foo as foo inner join foo.Bar as bar where foo.SOME_FOREIGN_KEY = 42 The SOME_FOREIGN_KEY column is not the name of something that Hibernate knows is mapped. For some reason, the first HQL query works, but the second one does not. My goal here is to get the second version to work, without traversing the object graph to the object identified by the foreign key. For this test, I have a known ID and I only want the objects