How can I join with a SQL view in a Hibernate query?
I have created a SQL view that calculates a function of two table rows. I want to use the view to limit the output of a hibernate query, but am not sure of the best way to do that.
The view encapsulates a stored function:
CREATE FUNCTION f(ax, ay, bx, by) AS ... ;
CREATE VIEW f_view AS
SELECT a.id a_id, b.id b_id, f(a.x, a.y, b.x, b.y) f FROM thing a, thing b;
Now I want to write a hibernate query s开发者_运维百科imilar to this SQL query:
SELECT Thing t1
INNER JOIN Thing t2 ...
INNER JOIN ...
INNER JOIN f_view v ON v.a_id = t1.id AND v.b_id = t2.id
WHERE t2.id = ? AND ... AND v.f < ?
Is it reasonable to use such a view in a hibernate query?
Your only options here are:
Map your
f_view
as (most likely immutable) entity in Hibernate - you can then include it in your HQL query.Write your query as SQL instead and map its results to your (managed or unmanaged) entities. Chapter 18 of Hibernate reference documents all the possibilities here.
精彩评论