hql

Indexed element access in JPQL

[亡魂溺海] 提交于 2019-12-06 05:19:11
Is it possible to do indexed element access in JPQL like in HQL : select o from Order o where o.items[0].id = 1234 I couldn't find something related in the JPA 2 specs, I am targeting EclipseLink JPA here, so if you come up with an EclipseLink solution, that's ok as well, although a JPQL standard solution is preferred. The INDEX function should do the trick (actually I tested it and it does): SELECT o FROM Order o JOIN o.items i WHERE i.id = 1234 AND INDEX(i) = 0 From the JPA 2.0 specification ( 4.6.17.2.2 Arithmetic Functions ): The INDEX function returns an integer value corresponding to the

Hibernate hql - help querying foreign key

喜你入骨 提交于 2019-12-06 04:48:33
问题 I am trying to query a foreign key patientId from table appointments. my Appointment object is mapped to my Patient object (don't know if it matters for the hql) like so: <many-to-one name="patient" class="application.model.Patient" fetch="select"> <column name="patientId" not-null="true" /> </many-to-one> and my query is: createQuery("from Appointment as appt where appt.patientId = 1").list(); I have tried to do joins like: createQuery("from Appointment as appt join appt.patientId ptid where

I can't make a inner join between two tables in hibernate hql query

匆匆过客 提交于 2019-12-06 04:47:35
I am new with this. Please help me. My inner join looks like this: select p.idprodus, p.denumire, p.cantitate from Produs p inner join Furnizor f on p.idfurn = f.idfurn I want to make the inner join on the column idfurn, but I get these errors: org.hibernate.QueryException: outer or full join must be followed by path expression select p.idprodus, p.denumire, p.cantitate from sakila.entity.Produs p inner join Furnizor f on p.idfurn = f.idfurn at org.hibernate.hql.classic.FromParser.token(FromParser.java:170) at org.hibernate.hql.classic.ClauseParser.token(ClauseParser.java:86) at org.hibernate

Subquery using derived table in Hibernate HQL

拈花ヽ惹草 提交于 2019-12-06 03:58:32
问题 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) 回答1: Unfortunately no, derived tables don't currently work in HQL. For example, the following works: List<int>

Hibernate property based on sum

北城以北 提交于 2019-12-06 02:51:29
I have figured out that I can use hibernate to get the sum of a number of entities using HQL as follows... public Long getEnvelopeTotal(AbstractEnvelope envelope) { String query = "select sum(t.amount) from User_Transaction t"; Long result = (Long) hibernateUtil.getSession().createQuery(query).uniqueResult(); return result; } Currently the rest of my application is able to seamlesly navigate the database via the object graph only. The problem with having to use the function above is that I have to do the following psuedo code... Get instance of Entity "Envelope" Pass Envelope instance i Based

row_number() over partition in hql

余生长醉 提交于 2019-12-06 02:29:53
问题 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 . 回答1: As far as I know you

NHibernate - HQL “join fetch” with SetMaxResults throws error

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 02:26:37
问题 I'm trying to run the simplest query: string queryStr = "select b " + "from Blog b " + "left outer join fetch b.BlogComments bc"; IList<Blog> blogs = Session.CreateQuery(queryStr) .SetMaxResults(10) .List<Blog>(); But it throws the following error: System.ArgumentNullException: Value cannot be null. Parameter name: source However, if I remove the 'fetch' from the HQL it works fine. Also if I leave fetch in but remove SetMaxResults it also works fine. It's something to do with the fetch +

Hibernate and dry-running HQL queries statically

天涯浪子 提交于 2019-12-06 02:13:04
问题 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

Spring MVC 3.1.2 + Jackson 2: LazyInitializationException when lazily initialize a collection - no session or session was closed

扶醉桌前 提交于 2019-12-06 02:00:33
I'm getting crazy with an error that I'm having using Spring MVC 3.1.2 and Jackson 2. I have the following model Class: @Entity @Table(name = "USER") @JsonIgnoreProperties(ignoreUnknown=true) public class User implements Serializable { @Id @SequenceGenerator(name = "USER_ID", sequenceName = "USER_ID_SEQ", allocationSize = 1) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_ID") private Long id; @Column(length = 50, nullable = false) private String firstName; @Column(length = 50, nullable = false) private String lastName; @ManyToMany @JoinTable(name = "FRIENDS", joinColumns

hql query to retrieve top n from each group

怎甘沉沦 提交于 2019-12-06 01:56:46
i want to achieve the following in HQL - select top n from table1 where table1.id IN (some select sub query) i have found the setMaxResult(n) method, but it can only retrieve top n of the entire result.. but what i want to achieve is top n of each group... Thanks... Here is JPQL you need: select a from Employee a where ( select count(*) from Employee b where a.department = b.department and a.salary <= b.salary ) <= 10 order by salary DESC 来源: https://stackoverflow.com/questions/4264528/hql-query-to-retrieve-top-n-from-each-group