[英]Spring data jpa calling stored procedure with lots of parameters
Is it possible to map an object to all the parameters inside a NamedStoredProcedureQuery
?是否可以将对象映射到
NamedStoredProcedureQuery
所有参数? I need to call a stored procedure that has over 40 parameters that it takes in. The following code becomes really long and hard to read:我需要调用一个包含超过 40 个参数的存储过程。以下代码变得非常长且难以阅读:
@NamedStoredProcedureQuery(
name="SomeSPName",
procedureName="SomeSPName",
resultClasses={ MyEntity.class },
parameters = {
@StoredProcedureParameter(name="PACKAGE", type=String.class, mode=ParameterMode.IN),
@StoredProcedureParameter(name="APPNM", type=String.class, mode=ParameterMode.IN),
// 40 more required parameters here...
)
public interface MyRepository extends JpaRepository<MyEntity, Long> {
@Query
@Procedure(name="SomeSPName")
long getResult(@Param("PACKAGE") String package, @Param("APPNM") String appnm, /* The rest of the params here */);
}
So my question is can I replace the arguments inside getResult
with an object that has all the getters and setters for the parameters and Hibernate will automatically assign the parameters correctly.所以我的问题是我可以用一个对象替换
getResult
的参数,该对象具有参数的所有 getter 和 setter,并且 Hibernate 将自动正确分配参数。 For example:例如:
public interface MyRepository extends JpaRepository<MyEntity, Long> {
@Query
@Procedure(name="SomeSPName")
long getResult(MyObject params);
}
Here, MyObject
will have fields corresponding to each of the StoredProcedureParameter
defined up top.在这里,
MyObject
将具有与顶部定义的每个StoredProcedureParameter
对应的字段。 Hopefully, this makes sense.希望这是有道理的。 I just don't want to put all the 40+ parameters in the
getResult
definition.我只是不想将所有 40 多个参数都放在
getResult
定义中。 I'm using DB2 if it makes a difference.如果它有所作为,我正在使用 DB2。 Any help would be appreciated!
任何帮助,将不胜感激!
EntityManager entityManager;
public Long getResult(Params params) {
StoredProcedureQuery storedProcedure = entityManager
.createStoredProcedureQuery("SomeSPName", Long.class)
.registerStoredProcedureParameter("PACKAGE", String.class, ParameterMode.IN)
.registerStoredProcedureParameter("APPNM", String.class, ParameterMode.IN)
//...
.setParameter("PACKAGE", params.getPackage())
.setParameter("APPNM", params.getAppNm())
//...
;
return storedProcedure.getSingleResult();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.