How to call oracle procedure from spring data or hibernate

时间:2018-11-13 07:49:09

标签: hibernate spring-data-jpa

How to call oracle predefined stored procedure which is not associated to any specific entity class.

This stored procedure does not return anything. Can this be done from spring data jpa or hibernate. If yes then how?

2 个答案:

答案 0 :(得分:0)

hibernate-jpa公开了一个@NamedStoredProcedureQuery批注,您可以使用它:

@Table(name = "TableName")
@Entity
@NamedStoredProcedureQuery(
    name = "proc_name",
    procedureName = "proc_name",
    resultClasses = {ResultDTO.class},
    parameters = {
          @StoredProcedureParameter(mode = ParameterMode.IN, type = BigInteger.class, name = "param")
    }
)
public class ResultDTO { ... }

您必须将其注释为表和实体,因此我总是只将其标记为要用作proc根目录的表。

答案 1 :(得分:0)

其中一种方法可以像使用JDBC connection一样

connection().prepareCall("{call storedProcedureName()}").execute()

也可以从hibernate sessionFactory connection

完成
(sessionFactory.currentSession as SessionImpl).connection().prepareCall("{call storedProcedureName()}").execute()

spring-web-hibernate的详细代码

https://gist.github.com/lynas/20d61e9b4e74155276a141a7f156bfe6