对不返回数据的存储过程使用 @NamedStoredProcedureQuery
Posted
技术标签:
【中文标题】对不返回数据的存储过程使用 @NamedStoredProcedureQuery【英文标题】:using @NamedStoredProcedureQuery for stored proc which do not return data 【发布时间】:2021-03-19 11:33:53 【问题描述】:我需要执行不返回任何数据的存储过程调用,我尝试使用 NamedStoredProcedureQuery 但它失败了
java.lang.IllegalArgumentException:没有找到具有该名称的@NamedStoredProcedureQuery:send_message
SendMessage.java
@NamedStoredProcedureQuery(
name = "send_message",
procedureName = "send_message",
resultClasses = SendMessage.class,
parameters =
@StoredProcedureParameter(mode = ParameterMode.IN, type = Date.class, name = "date"),
@StoredProcedureParameter(mode = ParameterMode.IN, type = String.class, name = "message")
)
public class SendMessage implements Serializable
MessageDao.java
@Component
public class MessageDao
@PersistenceContext
private EntityManager entityManager;
public void sendMessage(SendRequest sendRequest)
StoredProcedureQuery query = this.entityManager.createNamedStoredProcedureQuery("send_message")
.registerStoredProcedureParameter(0 , Date.class , ParameterMode.IN)
.registerStoredProcedureParameter(1 , String.class, ParameterMode.IN);
query.setParameter(0, sendRequest.getBusinessDate());
query.setParameter(1, sendRequest.getMessage());
query.execute();
遇到异常
java.lang.IllegalArgumentException: No @NamedStoredProcedureQuery was found with that name : send_message
at org.hibernate.internal.SessionImpl.createNamedStoredProcedureQuery(SessionImpl.java:3586) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:362) ~[spring-orm-5.3.4.jar:5.3.4]
at com.sun.proxy.$Proxy88.createNamedStoredProcedureQuery(Unknown Source) ~[na:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:311) ~[spring-orm-5.3.4.jar:5.3.4]
at com.sun.proxy.$Proxy88.createNamedStoredProcedureQuery(Unknown Source) ~[na:na]
有什么办法可以使这项工作,我不能把 @Entity 放在 SendMessage.java 中,因为它不与任何数据库表关联,也不返回任何东西?
【问题讨论】:
【参考方案1】:注解@NamedStoredProcedureQuery
仅在您将其放在实体上时才有效。
你没有可以放置它的实体吗?
或者,您可以在src/resources/META-INF
中创建一个orm.xml
文件并将其放在那里:
<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://www.datanucleus.org/xsd/jpa/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.datanucleus.org/xsd/jpa/orm
http://www.datanucleus.org/xsd/jpa/orm_2_2.xsd" version="2.2">
<named-stored-procedure-query name="send_message" result-class="SendMessage"
procedure-name="send_message">
<parameter direction="IN" name="date" type="Date"/>
<parameter direction="IN" name="message" type="String"/>
</named-stored-procedure-query>
</entity-mappings>
【讨论】:
以上是关于对不返回数据的存储过程使用 @NamedStoredProcedureQuery的主要内容,如果未能解决你的问题,请参考以下文章