-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Generic Native Queries in 5.2 with DTOs
PostPosted: Fri May 26, 2017 6:23 pm 
Newbie

Joined: Fri May 26, 2017 6:15 pm
Posts: 3
Under Hibernate < 5.2, it was possible to have generic SQL queries like
Code:
    String sql = "select a, b, sum (c) csum from a group by a, b";
    SQLQuery q = session.createSqlQuery (sql);
    q
        .addScalar ("a", IntegerType.INSTANCE)
        .addScalar ("b", IntegerType.INSTANCE)
        .addScalar ("csum", IntegerType.INSTANCE);
    q.setResultTransformer (new AliasToBeanResultTransformer (RankingModel.class));
    List<RankingModel> results = q.list ();

where RankingModel looks like:
Code:
    public class RankingModel
    {
        public int a, b, csum;
    }

However, with 5.2, addScalar(), setResultTransformer() have all been deprecated, with the recommendation to use session.createNativeQuery() instead. The nearest equivalent to the above I have is:
Code:
    String sql = "select a, b, sum (c) csum from a group by a, b";
    NativeQuery<RankingModel> q = session.createNativeQuery (sql, RankingModel.class);
    List<RankingModel> results = q.list ();

However, this code fails with:
Code:
    org.hibernate.MappingException: Unknown entity: ... RankingModel] with root cause
    org.hibernate.MappingException: Unknown entity: ... RankingModel
        at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620)
        at org.hibernate.engine.spi.SessionFactoryImplementor.getEntityPersister(SessionFactoryImplementor.java:335)
        at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.getSQLLoadable(SQLQueryReturnProcessor.java:358)
        at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processRootReturn(SQLQueryReturnProcessor.java:411)
        at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processReturn(SQLQueryReturnProcessor.java:378)
        at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.process(SQLQueryReturnProcessor.java:180)
        at org.hibernate.loader.custom.sql.SQLCustomQuery.<init>(SQLCustomQuery.java:71)
        at org.hibernate.engine.query.internal.NativeQueryInterpreterStandardImpl.createQueryPlan(NativeQueryInterpreterStandardImpl.java:70)
        at org.hibernate.engine.query.spi.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:213)
        at org.hibernate.internal.AbstractSharedSessionContract.getNativeQueryPlan(AbstractSharedSessionContract.java:550)
        at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:992)
        at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:148)
   ...

Anyone have any idea what I'm missing?


Top
 Profile  
 
 Post subject: Re: Generic Native Queries in 5.2.x with DTOs
PostPosted: Sat May 27, 2017 4:25 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You can use:

Code:
Transformers.aliasToBean( RankingModel.class )


as explained in the User Guide.

More, you can use the SELECT NEW clause:

Code:
CallStatistics callStatistics = entityManager.createQuery(
    "select new org.hibernate.userguide.hql.CallStatistics(" +
    "    count(c), " +
    "    sum(c.duration), " +
    "    min(c.duration), " +
    "    max(c.duration), " +
    "    avg(c.duration)" +
    ")  " +
    "from Call c ", CallStatistics.class )
.getSingleResult();


Top
 Profile  
 
 Post subject: Re: Generic Native Queries in 5.2 with DTOs
PostPosted: Mon May 29, 2017 4:31 pm 
Newbie

Joined: Fri May 26, 2017 6:15 pm
Posts: 3
The User Guide entry has:
Code:
List<PersonSummaryDTO> dtos = session.createSQLQuery(
    "SELECT p.id as \"id\", p.name as \"name\" " +
    "FROM Person p")
.setResultTransformer (Transformers.aliasToBean (PersonSummaryDTO.class));


However, in this example, createSQLQuery() and setResultTransformer() have both been marked as deprecated - which brings me back to the problem of an upgrade path. Is there a version of this that is not marked as deprecated?


Top
 Profile  
 
 Post subject: Re: Generic Native Queries in 5.2 with DTOs
PostPosted: Tue May 30, 2017 1:05 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Instead of createSQLQuery, use createNativeQuery. For the ResultTransformer, a new version will be added in 6.0. Meanwhile, you have no choice but to use it.


Top
 Profile  
 
 Post subject: Re: Generic Native Queries in 5.2 with DTOs
PostPosted: Tue May 30, 2017 11:48 pm 
Newbie

Joined: Fri May 26, 2017 6:15 pm
Posts: 3
Thanks. It's good to know that it will be resolved in 6.x


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.