過去にこれを達成したのは、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);
}
}