hql

java hibernate: selecting the discriminator column in polymorphic hql query

我是研究僧i 提交于 2019-12-03 23:18:44
In hibernate, I want to select the discriminator value. Something like select discriminator, id, name, age from Animal The idea is to send the result of this query to the client side, so that I can display a different icon based on the value of the discriminator column (i.e. cat, dog, elephant, etc). Is that possible? How? You can do it as follows: select a.class, a.id, a.name, a.age from Animal a From Hibernate Documentation : The special property class accesses the discriminator value of an instance in the case of polymorphic persistence. Hibernate query objects, does not know columns. So

How hibernate works with HQL query if the Transformer is not explicit declared

冷暖自知 提交于 2019-12-03 22:58:30
I don't know where I can find the implementation mechanism of hibernate. I have a lot of questions about hibernate but we can start them from this one: If there is a HQL like this: from B b where b.x =: x and b.y =: y And query code like this: Query query = session.createQuery(hql.toString()); What is the default transformer to set all of the fields into B? I found this even doesn't need setter or getter to set values. Or say, what is the differece between it and this one: Query query = session.createQuery(hql.toString()).setResultTransformer(Transformers.aliasToBean(B.class)); Thank you for

Simple hql named query that uses a inner join

南楼画角 提交于 2019-12-03 21:47:55
I want to do something like this in my domain/entity object : @Entity @NamedQueries({ @NamedQuery(name="favouriteCats", query="from Cat c inner join on UserCat uc where uc.isFavourtie = true and uc.user = :user") }) public final class Cat extends BaseTable So that in my service layer I can do this : Query query = session.getNamedQuery("favouriteCats") query.setParameter(0, MyUser); return query.list(); However, my syntax in HQL is incorrect - and aftern ten minutes looking at official docs I have decided to give up and ask here ... ? My usercat table is joined like so : @ManyToOne(cascade =

New Object with HQL - NPE on StandardAnsiSqlAggregationFunctions, determineJdbcTypeCode

六眼飞鱼酱① 提交于 2019-12-03 21:47:09
I know this has been asked a lot , but I seem to be having a different problem. I saw that there is a bug in Hibernate that struggles with a SumFunction ignoring custom user types, but I'm not using a custom user type to my knowledge. I'm basically trying to create a new object that is a roll-up of an existing mapping bean, however I receive a NullPointerException against some Hibernate dialect function. I tried using both Double and BigDecimal as my value field type, but both provide me the same error message on deployment. Please help! Here's some code: Mapped bean: @Entity @Table(name = "v

Spring data jpa. Find max if no result return default value

天涯浪子 提交于 2019-12-03 17:13:17
问题 I've implemented in my spring repository interface: @Query("SELECT max(ch.id) FROM MyEntity ch") Long getMaxId(); It works correctly if db is not empty. If I start my environment with test configuration (H2DB is used) - there is no data in the very beginning. And result returned by getMaxId() is null . I would like to have here 0 . Is it possible to modify my *JpaRepository to have 0 result? If yes, how it should be modified? 回答1: You can use coalesce like : @Query("SELECT coalesce(max(ch.id)

Dynamic HQL query using hql expressions rather than Criteria?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 17:04:45
I am trying to write a partially dynamic HQL query without resorting to the Criteria API for various reasons. I wanted to know if there is an easy way to short circuit a where restriction using HQLs expressions. For example, here is the original query which works fine: SELECT customer FROM Customer as customer INNER JOIN customer.profile as profile WHERE profile.status IN :statusCodes AND profile.orgId IN :orgIds StatusCodes is a list of Strings and orgIds is a list of Integers. However, either one is optional and shouldn't restrict if null is passed instead of a collection. I've tried to

Hibernate AliasToBean with Collection

▼魔方 西西 提交于 2019-12-03 16:47:52
I have a bi-directional one-to-many relationship defined between Parent and Child classes. I'm looking to execute a query such that I can return a single parent, and a subset of its children in a bean. public class Parent { private int id; private Set<Child> children = new HashSet<Child>(0); // other fields + getters and setters } public class Child { private Parent parent; private int age; // other fields + getters and setters } The output I'm looking to achieve is: public class ParentChildWrapper { private Parent parent; private Collection<Child> children; // subset of children } I'd like to

Getting error that unexpected token: ON near line 1, column 135

跟風遠走 提交于 2019-12-03 16:33:12
I am new to hibernate and trying to run query in hibernate but i am getting the exception that unexpected token: ON near line 1, column 135 [SELECT A.comp_id.appRefNo .... Here is the code StringBuffer query = new StringBuffer("SELECT A.comp_id.appRefNo, A.comp_id.custId from "); query.append(LosaCustContactZ.class.getName()); query.append(" A INNER JOIN " + LosaCust.class.getName() + " B ON ( B.comp_id.appRefNo = A.comp_id.appRefNo AND " + "B.comp_id.custId = A.comp_id.custId) INNER JOIN " + LosaApp.class.getName() + " C ON " + "(B.comp_id.appRefNo = A.comp_id.appRefNo) "); query.append(

Convert Hql to Sql Query without hibernate_show_sql=true

可紊 提交于 2019-12-03 16:27:24
问题 I am executing the following HQL and it is executing properly String hql = "FROM Employee"; Query query = session.createQuery(hql); List results = query.list(); Now i also want to log the sql generated at the backend in logs for support users , I want to make use of QueryTranslator please advise how can I generate the sql for corresponding HQL please advise how to achieve this. 回答1: You can use hibernate QueryTranslator: String hqlQueryString = hqlQuery.getQueryString();

Hibernate Pagination using HQL

北慕城南 提交于 2019-12-03 16:20:12
Hibernate Pagination Issue I have an issue which is related to Hibernate Pagination and to some extent this has been explained in Mysql Pagination Optimization Using Hibernate's ScrollableResults to slowly read 90 million records Hibernate - HQL pagination Issues with Pagination and Sorting Hibernate Row Pagination Details HQL Query from Application: Query q = session.createQuery("from RequestDao r order by r.id desc"); q.setFirstResult(0); q.setMaxResults(50); Query returns 3 million records and for pagination we are setting only 50 of those records, pagination page is very slow because on