我们可以在Spring data jpa @Query中调用或组合Select和过程吗

时间:2019-03-14 12:13:05

标签: java spring hibernate spring-data-jpa spring-data

我有一个用数据库编写的过程。 my_procedure(val1,val2)

所以,可以说我有一个这样的数据库查询:

MM_TO_UNITS

我想将其转换为Spring的Data JPA @Query。像这样:

select field1 as fieldName,  field2 as fieldId
(select * from mydb.my_procedure(id)) as aValue // A procedure call
from mydb.my_table

我们允许这样做吗?

更新

例如,我有以下要转换的查询。

@Query (" HERE I WANT THE ABOVE QUERY TO IMPLEMENT")
public List<MyTable> getDetails ()

我想做类似的事情

select id, name, roll,
(select * from db.calculate_fee (date, id)) fee
from Student
where  id = 1 AND roll = 5

反正有这样做吗?

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用本机查询?

@Query(nativeQuery = true, value = "select field1 as fieldName, field2 as fieldId...")
List<MyTable> getDetails();

(顺便说一句,由于存储库是接口,'getDetails'不需要public)

答案 1 :(得分:0)

@NamedStoredProcedureQueries({
        @NamedStoredProcedureQuery(name = "getAllEmployees",
                                    procedureName = "get_all_employees",
        resultClasses = Employees.class)
})
@Procedure(name = "getAllEmployees")
List<Employees> getAllEmployees();

Complete tutorial