Writing a query builder for any entity

2,112 views
Skip to first unread message

Dingo

unread,
Jan 13, 2013, 8:43:19 AM1/13/13
to quer...@googlegroups.com
We have a query criteria for web service and right now have logic to transform this XML to hibernate criteria. It's not a DSL sweet spot although we'd like to use the DSL to validate the request. Is this possible with the Q classes or via any of the querydsl classes?

Thanks

Timo Westkämper

unread,
Jan 15, 2013, 3:47:50 PM1/15/13
to quer...@googlegroups.com
Hi.

You can construct the Querydsl query manually and traverse the query model to do any needed validations.

For all Querydsl queries the metadata is contained in a QueryMetadata instance, with the following default implementation http://www.querydsl.com/static/querydsl/latest/apidocs/com/mysema/query/DefaultQueryMetadata.html

You wouldn't be using generated classes here, but lower level Querydsl expression types instead.

e.g. PathBuilder, which can be used for generic property access http://www.querydsl.com/static/querydsl/latest/apidocs/com/mysema/query/types/path/PathBuilder.html

Br,
Timo

Jean-Christophe Lagache

unread,
Jan 22, 2013, 6:16:06 AM1/22/13
to quer...@googlegroups.com
Hi,
in this blog post (http://koelec.blogspot.nl/2012/03/filter-expressions-in-rest-urls.html), the dsl expression is evaluate through MVEL (http://mvel.codehaus.org/).
It should be adapted whith non generated classes.

JC

Nathan Davenport

unread,
Feb 7, 2013, 1:46:49 AM2/7/13
to quer...@googlegroups.com
I am trying to evaluate if QueryDSL will solve a similar need for us.  We want to dynamically sort and filter entities based on properties of abstract parent properties as well as columns in joined entity tables specific to each entity.  FYI, we are using Spring Data JPA.  So far, based on other posts I am starting to see how this could work, although the fact that the QClasses don't extend the abstract Q class for the entities has kind of thrown me for a loop.  I think I can get around that later with a parameterized Predicate building helper class or the like.

However, being very new to querydsl, I am finding it hard to translate the other advice above into code.  I'd like to add a few things to my query without the Q classes, based on table and column name conventions we have in place.  For example I have a Category entity that extends our AbstractEntity base class that adds things like a global companyId.  I can do this as long as I know the QClass:

QAbstractEntity abstractEntity = new QAbstractEntity (QCategory.category);
Predicate predicate = abstractEntity.companyId.eq(companyId);
entityPage = getRepository().findAll(predicate, pageable);  //querying spring data jpa repository



I would like to add some additional things to the predicate.  Primarily, join in a one-to-many category_data table and sort by some of it's columns.  It would be similar to what Timo answered here...

http://forum.springsource.org/showthread.php?110466-Forming-a-QueryDSL-Predicate-through-a-OneToMany-join

...except I would need to do it abstractly, not using the QClasses and evaluating the join table name and columns as strings.

Any links to or samples of dynamically building this type of Predicate would be a great help.   If further examples of the classes or tables I am working with would help, please let me know, but at this point I'm only marginally sure my post makes any sense at all.  Thanks!










Timo Westkämper

unread,
Feb 7, 2013, 2:09:52 AM2/7/13
to Querydsl on behalf of Nathan Davenport
Hi Nathan.

On Thu, Feb 7, 2013 at 8:46 AM, Nathan Davenport via Querydsl <querydsl+noreply-APn2wQfJ7JowEBK...@googlegroups.com> wrote:
I am trying to evaluate if QueryDSL will solve a similar need for us.  We want to dynamically sort and filter entities based on properties of abstract parent properties as well as columns in joined entity tables specific to each entity.  FYI, we are using Spring Data JPA.  So far, based on other posts I am starting to see how this could work, although the fact that the QClasses don't extend the abstract Q class for the entities has kind of thrown me for a loop.  I think I can get around that later with a parameterized Predicate building helper class or the like.

The QClasses don't reflect the inheritance relationship of their entity counterparts directly, because eitherwise they would be generic and would look quite ugly.

For dynamic use cases use PathBuilder http://www.querydsl.com/static/querydsl/2.9.0/apidocs/com/mysema/query/types/path/PathBuilder.html

However, being very new to querydsl, I am finding it hard to translate the other advice above into code.  I'd like to add a few things to my query without the Q classes, based on table and column name conventions we have in place.  For example I have a Category entity that extends our AbstractEntity base class that adds things like a global companyId.  I can do this as long as I know the QClass:

QAbstractEntity abstractEntity = new QAbstractEntity (QCategory.category);
Predicate predicate = abstractEntity.companyId.eq(companyId);
entityPage = getRepository().findAll(predicate, pageable);  //querying spring data jpa repository


Using PathBuilder you get a more generic version

PathBuilder pathBuilder = new PathBuilder(type, variable);
Predicate predicate = pathBuilder.get("company").get("id").eq(companyId);
entityPage = getRepository().findAll(predicate, pagable);
 


I would like to add some additional things to the predicate.  Primarily, join in a one-to-many category_data table and sort by some of it's columns.  It would be similar to what Timo answered here...

http://forum.springsource.org/showthread.php?110466-Forming-a-QueryDSL-Predicate-through-a-OneToMany-join

...except I would need to do it abstractly, not using the QClasses and evaluating the join table name and columns as strings.

Any links to or samples of dynamically building this type of Predicate would be a great help.   If further examples of the classes or tables I am working with would help, please let me know, but at this point I'm only marginally sure my post makes any sense at all.  Thanks!

You can't declare explicit joins in a Predicate, that happens on the Query level, but you can use PathBuilders which are Paths for any task you would normally use QClasses and their properties: predicates, sorts, joins etc.

Hope this helps.

Timo

--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Timo Westkämper
Mysema Oy
+358 (0)40 591 2172
www.mysema.com


Nathan Davenport

unread,
Feb 7, 2013, 4:38:02 PM2/7/13
to quer...@googlegroups.com
Thanks for your quick response.  I'll try a more general approach using PathBuilder.

You can't declare explicit joins in a Predicate, that happens on the Query level

This would explain my difficulty in finding the information I was looking for.  It appears unfortunately, that I may not be able to use Spring Data JPA repositories directly for what I would like to do.  If they are in charge of the Query and the joins, then we've reached the technical limit of what it can accomplish, because it cannot correctly join and sort through OneToMany relationships.  Perhaps it can be fixed, but I have seen questions on this issue on SO that were read by the SD-JPA maintainers, but not answered. 

So I'll either have to move to a fully querydsl solution that or look into fixing SD-JPA (sorry, I'm just not that good Mr. Gierke)

Timo Westkämper

unread,
Feb 7, 2013, 5:33:28 PM2/7/13
to Querydsl on behalf of Nathan Davenport
Hi.


You can use Querydsl queries inside Spring Data repositories, you just need to go beyond the helper methods.

Br,
Timo
 

--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Nathan Davenport

unread,
Feb 7, 2013, 5:38:04 PM2/7/13
to Querydsl on behalf of Timo Westkämper
You can use Querydsl queries inside Spring Data repositories, you just need to go beyond the helper methods.

Good to know.  I'll check that out.  I know you end up dealing with a lot of Spring Data questions not directly related to querydsl and I appreciate that!
Reply all
Reply to author
Forward
0 new messages