-->
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: select e.*, dbfunction() from entity e... in Criteria API
PostPosted: Tue Jun 26, 2012 5:41 am 
Newbie

Joined: Wed Aug 24, 2011 4:17 am
Posts: 3
Hi all,

Suppose you have a HQL query like this
Code:
select e.*, dbfunction() from entity e
where e.field1=value1 and e.field2=value2...

with a complex and dynamic where clause, and also a dynamic select clause, I mean, in some situations I must do a call to a db function in the select clause.

Now, the question is: how would you do this with Criteria API? The problem is the dynamic call to the db function.

A call to a db function would be implemented with a projection. But if I use a projection I loose the properties of the root entity.

When I do
Code:
session.createCriteria(Entity.class, "e")

hibernate automatically expands all properties of the root entity. How do I combine this with projections?

It would be nice to have
Code:
criteria.addProjection(...);

instead of
Code:
criteria.setProjection(...);

and hibernate would combine the properties of the root entity with the projection.

What do you say?

Thanks in advance!
Rui Baeta


Top
 Profile  
 
 Post subject: Re: select e.*, dbfunction() from entity e... in Criteria API
PostPosted: Fri Jun 29, 2012 7:14 am 
Newbie

Joined: Wed Aug 24, 2011 4:17 am
Posts: 3
Hello, again

Thinking again, maybe a better way would be to have a special projection, say "RootEntityProjection", that mimics the behavior of "entity.*", i.e., it would automatically expand all properties of the specified entity.

This way, we could combine RootEntityProjection with SQLProjection, something like this:
Code:
session.createCriteria(Entity.class, "entity")
  .setProjection(Projections.projectionList()
    .add(Projections.rootEntityProjection(Entity.class, "entity"))   // this would expand into all root entity properties, aliased
    .add(Projections.sqlProjection("dbFunction() as alias", ..., ...))  // this would call dbFunction()
  )
.setResultTransformer(Transformers.aliasToBean(Entity.class));

I'm using alias-based ResultTransformer, so we must use aliases like property names, but you could enhance implicit org.hibernate.criterion.CriteriaSpecification#PROJECTION to take care of this.

And, of course, I am assuming we have a transient property declared in the entity to receive the dbFunction() result.

This behavior seems pretty much like when we use the @Formula annotation, except that in many situations, we want that to be dynamic, i.e., we don't always want to call the same database function or some times we don't want to call any database function at all. Additionally, a database function may have parameters and @Formula doesn't allow variables, only constants.

Any thoughts on this?

Thanks,
Rui Baeta


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.