当前CallableStatement ou不是ResultSet,但是调用了getResultList(MySQL)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当前CallableStatement ou不是ResultSet,但是调用了getResultList(MySQL)相关的知识,希望对你有一定的参考价值。
我正在使用Spring Boot进行projet,并且我试图使用MyQSL中的存储过程来计算一本书的评论数。
存储过程返回一个int值,它是注释的数量。
这是我的存储过程:
DROP PROCEDURE IF EXISTS countComments;
DELIMITER //
CREATE PROCEDURE countComments(IN idBook int, Out total int)
BEGIN
SELECT count(*) INTO total FROM comments WHERE id_book = idBook;
END //
DELIMITER ;
评论实体:
@Entity
@Table(name="comment")
@NamedStoredProcedureQueries(
@NamedStoredProcedureQuery(
name = "countComments",
procedureName = "countComments",
parameters =
@StoredProcedureParameter(name = "idBook",
mode = ParameterMode.IN,
type = Integer.class),
@StoredProcedureParameter(name = "total",
mode = ParameterMode.OUT,
type = Integer.class)
)
)
public class Comment implements Serializable
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id_comment", nullable=false)
private int idComment;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "id_book", nullable = false)
private Book book;
//constructor
//setters and getters
我使用了服务方法,这是我在控制器中调用的服务:
@PersistenceContext
private EntityManager entityManager;
public int countSeenByCollaborateur(int CollaborateurId)
StoredProcedureQuery theQuery = entityManager
.createNamedStoredProcedureQuery("countSeenByCollaborateur");
theQuery.setParameter("idCollaborateur", CollaborateurId);
BigDecimal outCount = (BigDecimal) theQuery.getSingleResult();
return outCount.intValue();
这里是错误:
java.lang.IllegalStateException: Current CallableStatement ou was not a ResultSet, but getResultList was called
at org.hibernate.procedure.internal.ProcedureCallImpl.getResultList(ProcedureCallImpl.java:720)
at org.hibernate.procedure.internal.ProcedureCallImpl.getSingleResult(ProcedureCallImpl.java:742)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:402)
at com.sun.proxy.$Proxy151.getSingleResult(Unknown Source)
at com.archi.demo.service.CommentService.countSeenByCollaborateur(CommentService.java:42)
at com.archi.demo.RepositoryTest.CommentRepositoryTest.testFetchData(CommentRepositoryTest.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
有人可以帮助我解决该问题,并提前致谢。
答案
如果您阅读StoredProcedureQuery
的javadoc,则会发现:
StoredProcedureQuery
方法用于检索通过getOutputParameterValue
和INOUT
参数从过程传回的值。
这意味着您的代码应类似于:
OUT
另一答案
更改
StoredProcedureQuery theQuery = entityManager.createNamedStoredProcedureQuery("countComments");
theQuery.setParameter("idBook", bookId);
int total = ((Number) theQuery.getOutputParameterValue("total")).intValue();
return total;
to
BigDecimal outCount = (BigDecimal) theQuery.getSingleResult();
以上是关于当前CallableStatement ou不是ResultSet,但是调用了getResultList(MySQL)的主要内容,如果未能解决你的问题,请参考以下文章