使用 return table、hibernate、postgresql 的函数
use a function which return table, hibernate, postgresql
我在 postgresql 数据库上有一个可用的存储函数
create or replace function sp1(d1 date, d2 date)
returns table(ServiceType varchar, counter bigint) as $$
begin
return query select servicerequesttype, count(*)as counter from events
where creationdate>=d1 and creationdate<=d2
group by servicerequesttype
order by(counter) desc;
end;
$$
language plpgsql;
which returns a table 有两列,varchar 和 bigint。我执行它就像
select * from sp1();
现在我想在休眠时使用它。据我了解,我想在休眠上执行原始查询并为结果创建一个列表。
这样可以吗,还是得把存储过程全部重写?
可以在数据库中创建视图,然后在Java代码中,可以查询视图
当您有复杂的查询或聚合值时,这是一种使用的技术。
@Entity
@Table(name = "V_SERVICE_VIEW")
public class ServiceView {
..
..
}
我在 postgresql 数据库上有一个可用的存储函数
create or replace function sp1(d1 date, d2 date)
returns table(ServiceType varchar, counter bigint) as $$
begin
return query select servicerequesttype, count(*)as counter from events
where creationdate>=d1 and creationdate<=d2
group by servicerequesttype
order by(counter) desc;
end;
$$
language plpgsql;
which returns a table 有两列,varchar 和 bigint。我执行它就像
select * from sp1();
现在我想在休眠时使用它。据我了解,我想在休眠上执行原始查询并为结果创建一个列表。
这样可以吗,还是得把存储过程全部重写?
可以在数据库中创建视图,然后在Java代码中,可以查询视图 当您有复杂的查询或聚合值时,这是一种使用的技术。
@Entity
@Table(name = "V_SERVICE_VIEW")
public class ServiceView {
..
..
}