hql

Map hibernate projections result to java POJO model

无人久伴 提交于 2019-12-04 09:29:49
I've been using spring and hibernate for this past few weeks and I've always been learning something new there. Right now I've got a problem that I want to solve with Projections in Hibernate. Suppose there is a model Person and that model has many Car . The following are how the class definitions roughly gonna look like: public class Person implements java.io.Serializable { private Integer id; private String name; private List<Car> cars; private Integer minYear; // Transient private Integer maxYear; // Transient } public class Car implements java.io.Serializable { private Integer id; private

Join map and refer to its key/value in HQL

蓝咒 提交于 2019-12-04 08:59:34
Suppose I have a map: <map name="externalIds" table="album_external_ids"> <key column="album_id" not-null="true"/> <map-key-many-to-many class="Major" column="major_id"/> <element column="external_id" type="string" not-null="true"/> </map> How do I make a HQL meaning "select entities where map key's id == :foo and map value == :bar"? I can join it using select album from Album album join album.externalIds ids But how would I then refer to ids' key and value? ids.key.id = :foo and ids.value = :bar doesn't work, and hibernate doc is silent on this topic. Naive approaches that didn't work: select

Subquery using derived table in Hibernate HQL

冷暖自知 提交于 2019-12-04 08:16:21
I have a Hibernate HQL question. I'd like to write a subquery as a derived table (for performance reasons). Is it possible to do that in HQL? Example: FROM Customer WHERE country.id in (SELECT id FROM (SELECT id FROM Country where type='GREEN') derivedTable) (btw, this is just a sample query so don't give advice on rewriting it, is just the derived table concept I'm interested in) Unfortunately no, derived tables don't currently work in HQL. For example, the following works: List<int> result = nHSession.CreateQuery( @"select distinct Id from User u") .List<int>().ToList(); ...the following

row_number() over partition in hql

人盡茶涼 提交于 2019-12-04 07:54:29
What is the equivalent of row_number() over partition in hql I have the following query in hql: select s.Companyname, p.Productname, sum(od.Unitprice * od.Quantity - od.Discount) as SalesAmount FROM OrderDetails as od inner join od.Orders as o inner join od.Products as p " + "inner join p.Suppliers as s" + " where o.Orderdate between '2010/01/01' and '2014/01/01' GROUP BY s.Companyname,p.Productname" I want to do partition by s.Companyname where RowNumber <= n . As far as I know you cannot use row_number() neither in HQL nor in JPQL . I propose to use a native SQL query in this case:

Hibernate and dry-running HQL queries statically

只谈情不闲聊 提交于 2019-12-04 07:32:09
I'd like to "dry-run" Hibernate HQL queries. That is I'd like to know what actual SQL queries Hibernate will execute from given HQL query without actually executing the HQL query against real database. I have access to hibernate mapping for tables, the HQL query string, the dialect for my database. I have also access to database if that is needed. Now, how can I find out all the SQL queries Hibernate can generate from my HQL without actually executing the query against any database? Are there any tools for this? Note, that many SQL queries can be generated from one HQL query and the set of

Ordinal Parameter Not bound : 2 in @Query annotation

陌路散爱 提交于 2019-12-04 06:43:45
问题 I am getting following error when I try to run this query. org.hibernate.QueryException: Ordinal parameter not bound : 2", @Query(value = "SELECT amu " + "FROM Upgrade amu " + "INNER JOIN FETCH amu.visibility v " + " WHERE v IN ?2 " + "AND amu.id= '?1' " ) Optional<Upgrade> myFindMethod(final String uid, final String cid); If I change " WHERE v IN ?2 " with " WHERE v IN ?2 OR v IN ?1 " , then it works. I have no idea why it does not work. Any idea? Note: Visibility is type of Set<String> in

How to execute normal SQL query in Hibernate without HQL?

巧了我就是萌 提交于 2019-12-04 05:48:55
问题 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

How to avoid unnecessary selects and joins in HQL and Criteria

假如想象 提交于 2019-12-04 05:32:18
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())) .list(); Wich produces this SQL: select this_.id as id1_1_, this_.description as descript2_1_1_,

Collection.contains(Enum.Value) in HQL?

蓝咒 提交于 2019-12-04 05:15:42
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").list(); Or am I stuck fetching all Foo instances and filtering them out at the DAO level? List foos =

Hibernate having difficulty with '@' character in HQL

痞子三分冷 提交于 2019-12-04 04:41:18
问题 Working with hibernate and spring social, I am trying to query the database by email address. when i do this query: public Account findAccountByUsername(String username) { Session session = sessionFactory.getCurrentSession(); String selectQuery = "FROM Account as account WHERE account.username = "+username; Query query = session.createQuery(selectQuery); @SuppressWarnings("unchecked") List<Account> results = query.list(); if (!results.iterator().hasNext()) return null; return results.iterator