2017-05-11 16 views
0

結果セットと多重出力パラメータを持つSpring Data JPAを使用してストアドプロシージャを呼び出すことができるかどうかを知りたいと思います。それが解決された場合Springデータを使用したスト​​アドプロシージャの呼び出しJPA

私は同じhttps://github.com/spring-projects/spring-data-examples/issues/80

のためのGitの問題を発見し、誰かが春ブーツとの一例を提供することができますか?

答えて

0

過去にこれを達成したのは、Spring Data JPAリポジトリ(link)にカスタム動作を追加することです。内部では、EntityManagerを取得し、java.sql.ConnectionとCallableStatementを使用します。

編集:高レベルのサンプルコードを追加します。サンプルは、あなたがEntityRepositoryに持っていると仮定すると、あなたがよく

として他の人に適用可能であるべきであるが、休止状態のアイデアを使用しているという仮定を行い

public interface EntityRepositoryCustom { 

    Result storedProcCall(Input input); 
} 

public class EntityRepositoryImpl implements EntityRepositoryCustom { 

    @PersistenceContext 
    private EntityManager em; 

    @Override 
    public Result storedProcCall(Input input) { 
     final Result result = new Result(); 
     Session session = getSession(); 
     // instead of anonymous class you could move this out to a private static class that implement org.hibernate.jdbc.Work 
     session.doWork(new Work() { 

      @Override 
      public void execute(Connection connection) throws SQLException { 
       CallableStatement cs = null; 
       try { 
        cs = connection.prepareCall("{call some_stored_proc(?, ?, ?, ?)}"); 
        cs.setString(1, ""); 
        cs.setString(2, ""); 
        cs.registerOutParameter(3, Types.VARCHAR); 
        cs.registerOutParameter(4, Types.VARCHAR); 
        cs.execute(); 

        // get value from output params and set fields on return object 
        result.setSomeField1(cs.getString(3)); 
        result.setSomeField2(cs.getString(4)); 
        cs.close(); 

       } finally { 
        // close cs 
       } 
      } 
     }); 
     return result; 
    } 

    private Session getSession() { 
     // get session from entitymanager. Assuming hibernate 
     return em.unwrap(org.hibernate.Session.class); 
    } 

} 
関連する問題