-->
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.  [ 13 posts ] 
Author Message
 Post subject: Get metadata of HQL query entities/fields
PostPosted: Wed Jul 05, 2017 4:41 am 
Newbie

Joined: Wed Jul 05, 2017 4:09 am
Posts: 7
Hello, i'm using hibernate 5.2.

Given an arbitrary HQL query like this:

Code:
Query q = session.createQuery(
  "SELECT P.id, P.name, P.age, A.city, A.street FROM Person P LEFT JOIN Address A ON P.addressId = A.id");


does it exist a way (before loading the data) to ask to the Query object (q) for the list of returned fields names in form of entity+attribute name?

For the given example I would like to obtain something like:

"Person.id"
"Person.name"
"Person.age"
"Address.city"
"Address.street"

I need a way to dynamically analyze a hql query and determine the "source" of each returned value.

Thank you.


Top
 Profile  
 
 Post subject: Re: Get metadata of HQL query entities/fields
PostPosted: Wed Jul 05, 2017 5:51 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
Quote:
does it exist a way (before loading the data) to ask to the Query object (q) for the list of returned fields names in form of entity+attribute name?


I don't think there's anything you could to get the list of fields to be returned.

Quote:
I need a way to dynamically analyze a hql query and determine the "source" of each returned value.


What is it that you are trying to achieve with this feature?


Top
 Profile  
 
 Post subject: Re: Get metadata of HQL query entities/fields
PostPosted: Wed Jul 05, 2017 6:36 am 
Newbie

Joined: Wed Jul 05, 2017 4:09 am
Posts: 7
We are developing a complex product.

Our persistent domain entities are "annotated" with a lot of extra information (metadata),
such as labels to display on UI, weak references, and so on.

This metadata is needed to dynamically build the UI, and to implement some automatic UI behaviors.

I need to extract such metadata also on HQL queries written by the end user, which are used to display data on the UI.

I hope I explained myself.


Top
 Profile  
 
 Post subject: Re: Get metadata of HQL query entities/fields
PostPosted: Wed Jul 05, 2017 6:58 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
Quote:
I need to extract such metadata also on HQL queries written by the end user, which are used to display data on the UI.


You can extract the metadata using Java reflection and then match it against what the User sends to the DAO layer. That's not Hibernate related in any way, it's just a custom query building alternative.

And while at it, make sure you use the Criteria API since you don't want to end up concatenating String and exposing your app to SQL Injection attacks.


Top
 Profile  
 
 Post subject: Re: Get metadata of HQL query entities/fields
PostPosted: Wed Jul 05, 2017 7:02 am 
Newbie

Joined: Wed Jul 05, 2017 4:09 am
Posts: 7
Quote:
You can extract the metadata using Java reflection and then match it against what the User sends to the DAO layer


Yes, the problem is how to match it! I need the entities/properties names of returned values of HQL query.


Top
 Profile  
 
 Post subject: Re: Get metadata of HQL query entities/fields
PostPosted: Wed Jul 05, 2017 7:04 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
Just use Java Reflection for that. You can then match the annotated property and build a Map of properties to their UI labels.


Top
 Profile  
 
 Post subject: Re: Get metadata of HQL query entities/fields
PostPosted: Wed Jul 05, 2017 8:27 am 
Newbie

Joined: Wed Jul 05, 2017 4:09 am
Posts: 7
I don't understand. I have a query string like SELECT P.name FROM Person P
I (already) have all the metadata available for the entity attribute Person.name
Now I have to match P.name with Person.name, how is it possible?


Top
 Profile  
 
 Post subject: Re: Get metadata of HQL query entities/fields
PostPosted: Wed Jul 05, 2017 9:17 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
Quote:
Now I have to match P.name with Person.name, how is it possible?


Unless you write a JPQL parser, you won't be able to know it. And what about custom SQL queries. However, you can solve this with DTO projections and the mapping between a given property/column to a DTO property is done through the query. You just have to associate the DTO projection to the entity metadata, and you are done. Easy peasy!


Top
 Profile  
 
 Post subject: Re: Get metadata of HQL query entities/fields
PostPosted: Wed Jul 05, 2017 9:29 am 
Newbie

Joined: Wed Jul 05, 2017 4:09 am
Posts: 7
Thank you.

Yes, unfortunately I think I need to parse the HQL/JPQL query, if the Query object does not offer me such information.

Yes, currently we already use DTO classes, but they are to much inflexible (hard coded).


Top
 Profile  
 
 Post subject: Re: Get metadata of HQL query entities/fields
PostPosted: Wed Jul 05, 2017 9:57 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
Quote:
Yes, unfortunately I think I need to parse the HQL/JPQL query, if the Query object does not offer me such information.


No, you don't. That will not solve the case when you have native SQL queries, which you need for any non-trivial project.

Quote:
Yes, currently we already use DTO classes, but they are to much inflexible (hard coded).


They are not inflexible and they will work for any type of query you have. Every DTO will be responsible for the associated entity property. It's easier to do it this way and effective too.


Top
 Profile  
 
 Post subject: Re: Get metadata of HQL query entities/fields
PostPosted: Wed Jul 05, 2017 10:01 am 
Newbie

Joined: Wed Jul 05, 2017 4:09 am
Posts: 7
Would it be possible to add this feature to the Query class? I think hibernate already does parsing and already has this information.

I found the (deprecated) methods:
query.getReturnAliases()
query.getReturnTypes()

It would be useful for me something like:
query.getReturnAttributes()

Thank you!


Top
 Profile  
 
 Post subject: Re: Get metadata of HQL query entities/fields
PostPosted: Wed Jul 05, 2017 10:16 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
This method will be removed in 6.0. As for getReturnAttributes, I don't see any reason why we should expose Hibernate internals. As I already pointed out, there are better ways to deal with this issue.


Top
 Profile  
 
 Post subject: Re: Get metadata of HQL query entities/fields
PostPosted: Wed Jul 05, 2017 11:50 am 
Newbie

Joined: Wed Jul 05, 2017 4:09 am
Posts: 7
Thank you again for your time and your answers.

My requirement is:

Give to the "end user" the ability to query (select only) data from a "query model" (which is not the real entities model, but a query model like in CQRS).
The "end user" is a person who uses HQL (no java, no compiler) to configure some applications at installation time.
These "configured" applications are able to automatically build the UI and load/display the data, but they need some "metadata" to do this.
Metadata is already available, and is bound to query model entities/attributes.
So, again, I need a way to analyze the returned fields in HQL queries and match them with entities/attributes, so I can get the metadata.

Thanks again.


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