-->
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.  [ 2 posts ] 
Author Message
 Post subject: How do I set column types on scalar Criteria queries?
PostPosted: Mon Jan 23, 2012 11:01 am 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
The title says it all, here's the use case:

I have a scalar Criteria query, like this:
Code:
class Result {
  public String pk;
  public BigInteger count; // Here comes trouble.
}
Criteria cr =
  getCurrentSession().createCriteria(Entity.class)
  .setProjection(Projections.projectionList()
    .add(Projections.groupProperty("entity_pk"), "pk") // Returns proper String type
    .add(Projections.rowCount()) // returns Long instead of BigDecimal
  ).setResultTransformer(Transformers.aliasToBean(Result.class));
I'm having trouble with the rowCount projection: it returns a Long, but the member variable is a BigInteger so the aliasToBean transformer will fail with an incompatible type.
I could roll my own transformer (using Apache Commons BeanUtils, which do this kind of conversion automatically), but I'd prefer to make the Criteria return the wanted type right away.


Top
 Profile  
 
 Post subject: Re: How do I set column types on scalar Criteria queries?
PostPosted: Tue Jan 24, 2012 9:30 am 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
After a lengthy debug trace, the options seem to be:

* use an SQLProjection (should work)
* write a subclass of Projection that does the type conversion (not sure whether that's feasible)
* write your own subclass of Dialect that specifies SQLFunctions that have the return type that you want (lots of work, even if you use an existing Dialect, and I'm not sure where to convert the JDBC result)

I found no way to make Hibernate convert as needed.
(Using Long is usually a mistake, all using Short/Integer/Long means type errors when the column gains or loses digits, sticking with BigInteger avoids this kind of hassle.)

Seems like a lot of details differ between scalar and entity Criteria queries.
(And I hate it so much...)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.