This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Stored Procedure inside HQL query? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Stored Procedure inside HQL query?" Watch "Stored Procedure inside HQL query?" New topic

Stored Procedure inside HQL query?

Cj Recto

Joined: Mar 02, 2012
Posts: 26
Hellow guys,

I have no Idea on how to do this in HQL
let's say in SQL I have this query :

where in getClientName is a stored procedure.

So, how do I do that in HQL?

Can we combine HQL query and stored procedure from native sql? I dont think it's possible.

Or so, can I create stored procedure in HQL and call it in my hibernate query?

So far what I'm doing is I just create a sub query so I can get the result in HQL:

this is my code for HQL :

But what I wanted to do is to create a stored procedure in hql to replace the sub query I used.
Is it possible?

I would appreciate any help!

Bogdan Baraila
Ranch Hand

Joined: May 23, 2011
Posts: 43
I don't think that you can call stored procedures with HQL but in your particular case you could use a formula mapping.
For example:
<property name="clientName" lazy="true">
(select from your_table t where t.foreign_key=id)

In this particular case the "id" value will be the current object that your are loading id.
Cj Recto

Joined: Mar 02, 2012
Posts: 26
Atlast someone answered, I really need to know how to do this.

Thanks for that answer , and I was able to execute one. But I have another question,
That formula mapping is not a global access, it is only particular to a certain table.

What if I have many tables that has a column entityId, so it means that I have to create formula for every table?
I guess that is a tedious work. Is there no way to create a global access or query, (just like if you have stored proc in native sql)
so I won't need to create formula in all of my tables that have entityId.

I agree. Here's the link:
subject: Stored Procedure inside HQL query?
It's not a secret anymore!