我正在使用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("idBook", BookId);

      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,您将发现:
getOutputParameterValue方法用于检索通过INOUTOUT参数从过程返回的值。
这意味着你的代码应该是:

StoredProcedureQuery theQuery = entityManager.createNamedStoredProcedureQuery("countComments");
theQuery.setParameter("idBook", bookId);
int total = ((Number) theQuery.getOutputParameterValue("total")).intValue();
return total;

07-27 17:21