java - 有什么方法可以在没有实体的情况下使用“@Procedure”注释?
问题描述
所以我想要一个“Void-Repository”,通过它可以访问不一定对实体进行操作的存储过程。
@Repository
public interface StoredProceduresRepository extends CrudRepository<Void, Long> {
@Procedure("my_answer_giver")
String getMyAnswer(@Param("input") String input);
}
但这当然行不通,因为它CrudRepository
期望Void
成为一个实体。
有没有一种方法可以使用@Procedure
注释而不必创建虚拟实体,或者我是否坚持使用EntityManager
通过准备好的语句进行查询的实现类?
因为老实说,这很丑陋:
@Repository
public class StoredProceduresRepository {
@PersistenceContext
EntityManager em;
public String getMyAnswer(String input) {
Query myAnswerGiver = em
.createStoredProcedureQuery("my_answer_giver")
.registerStoredProcedureParameter("input", String.class, ParameterMode.IN)
.setParameter("input", input);
Object result = ((Object[]) myAnswerGiver.getSingleResult())[0];
return (String) result;
}
}
解决方案
如果对您来说没问题,您可以使用您拥有的任何实体来代替 this Void
。那里提供的实体应该无关紧要。
public interface StoredProceduresRepository extends JpaRepository<SomeUnrelatedEntity, Long> {
@Procedure("my_answer_giver")
String getMyAnswer(@Param("input") String input);
}
推荐阅读
- python - 转换为 exe 时 Python pyinstaller 错误
- python - 如何在使用 id 作为参数的反向时解决 django 中的 NoReverseMatch?
- flutter - 输入'未来
' 不是颤振 Firestore 中类型 '() => void' 的子类型 - assembly - Qtspim:生成数组并更改值,语法错误
- data-analysis - 如果年龄变量的数据类型是对象,我想将其转换为数值。但我的模型中出现这样的错误
- flutter - 颤振地理位置没有显示任何内容
- python - 进行类继承时使用 super().__init__() 的问题
- javascript - JavaScript 显示隐藏的输入
- android - 有没有办法在横向或纵向模式下“冻结”应用程序?
- php - woocommerce_quantity_input 出现两次