hql

Hibernate how to use order by clause for sort by sum of field

喜你入骨 提交于 2019-12-10 21:38:47
问题 How to use order by clause for sort by sum of field Entity: class MyClass { private int a; private int b; //... } HQL works correctly: "SELECT myclass FROM MyClass myclass ORDER BY myclass.a + myclass.b DESC " How to do the same thing using Criteria API? 回答1: As you said you could use @Formula @Column(name="column_a") private Integer a; @Column(name="column_b") private Integer b; @Formula("(column_a+column_b)") private Integer c; After that you can sort by property c like criteria.addOrder

Querying computed fields in GORM

一曲冷凌霜 提交于 2019-12-10 21:34:47
问题 I am trying to query the following HQL using GORM: MailMessage.executeQuery("toId, count(toId) from (SELECT toId, threadId FROM MailMessage as m WHERE receiveStatus = 'u' GROUP BY threadId, toId) as x group by x.toId") The problem is that count(toId) is a computed field doesn't exist in MailMessage and that I am using a subquery. I get the following error: java.lang.IllegalArgumentException: node to traverse cannot be null! Ideally, I would like to use a generic executeQuery which will return

magic npe in hibernate

江枫思渺然 提交于 2019-12-10 18:56:30
问题 When I write Session session = sessionFactory.getCurrentSession(); List<Candidate> candidates = (List<Candidate>) session.createQuery( "select Candidate from Candidate as candidate left outer join" + " candidate.skills as skill where skill.id =" + 1).list(); I have npe at second line if I write Session session = sessionFactory.getCurrentSession(); List<Candidate> candidates = (List<Candidate>) session.createSQLQuery( "select candidate.* from candidate inner join candidate_skill" + " on

how does the new keyword in hql work?

余生长醉 提交于 2019-12-10 18:15:30
问题 I found this example in jboss's documentation. select new Family(mother, mate, offspr) from DomesticCat as mother join mother.mate as mate left join mother.kittens as offspr Where does the Family class come from. Do I need to import somewhere, or to use its fully qualified class name? 回答1: The Family is a plain POJO with the appropriate constructor which either needs to be declared or fully qualified. 回答2: Family must be a Java object that has an appropriate constructor. The import is

JPQL/HQL and JPA/Hibernate: boolean expression in select constructor expression not working (unexpected AST node: AND, NPE, HqlSqlWalker.setAlias)?

二次信任 提交于 2019-12-10 18:03:49
问题 I have a JPQL statement to return a schedule of sports games: SELECT NEW com.kawoolutions.bbstats.view.ScheduleGameLine( ga.id AS gid , ga.scheduledTipoff AS scheduledtipoff ... , sch.finalScore AS homefinalscore , sca.finalScore AS awayfinalscore , sch.finalScore IS NOT NULL AND sca.finalScore IS NOT NULL AS hasfinalscore ) I want the last expression (boolean) to evaluate to a boolean to indicate whether a game's final score has been completely reported or not (two entities of type Score,

Its possible insert row with embedded ID on table with HQL?

点点圈 提交于 2019-12-10 17:22:54
问题 I'm able to insert any row with HQL. Example: insert into MyMappedTable(field,field,field) select c.x, c.y, c.z from Object c where .... But, my requirement is insert with embedded Id @JoinColumn(insertable = false, name = "CATEGORYID", referencedColumnName = "ID", updatable = false) @ManyToOne(fetch = FetchType.EAGER, optional = false) private Category category; @EmbeddedId protected CategoryProductPK categoryProductPK; @Basic(optional = true) @Column(name = "POSITION") private Integer

FETCH JOIN maximum depth?

谁都会走 提交于 2019-12-10 15:45:20
问题 W was trying to fetch join over three levels: JOIN FETCH entity1.collection1.collection2 // two OneToMany relations but got: org.hibernate.HibernateException: Errors in named queries: [...] Is it because it was too deep, or because a collection of collections cannot be fetched this way? My max fetch depth is 3, if this is relevant. I can, at the same time, do a triple JOIN FETCH starting from the other side: JOIN FETCH entity3.entity2.entity1 // two ManyToOne relations Somehow I cannot find

Hibernate Criteria: find entity if any of its children's children have a specific property

孤街浪徒 提交于 2019-12-10 15:44:01
问题 I need to write a Criteria(or hql) to find a parent entity by a property of a child of its children entities. Here is my entities: // The top level parent class public class A { private Long id; private String someProperty; private B b; // and some other attributes... } // The second level parent class :) public class B { private Long id; private List<C> cList; // and some other attributes... } public class C { private Long id; private B b; private List<D> dList; // Other attributes.. }

How do you do a union of two tables in NHibernate?

风流意气都作罢 提交于 2019-12-10 15:35:02
问题 I need to do a union of two tables using NHibernate and HQL. I have found very little help online, and I want to know if it is possible and if so how? 回答1: Found my answer: http://www.hibernate.org/117.html#A21 It doesn't currently support union or intersect. 回答2: You could use a named sql-query and do the union in raw SQL. NHibernate will be able to populate entity instances from the sql-query and return those as the query result. See here and here. 回答3: I don't believe HQL supports unions,

HQL - named parameters as left hand side

谁说胖子不能爱 提交于 2019-12-10 14:59:36
问题 After we did our Spring Boot Upgrade from 1.5.13 to 2.0.2 some of our HQL queries failed with "Parameter [myParameter] not bound". In both cases we used Hibernate 5.2.12. After looking closer I found out that all queries that failed used named parameters on the left-hand side of a comparison. So @Query("select a from MyEntity a where :name = a.name") List<MyEntity> findByName (@Param("name") String name); worked wit Spring Boot 1.5.13, failed with Spring Boot 2.0.2 but @Query("select a from