Java 是否可以使用Hibernate 4从存储的T-SQL过程中获取两个输出参数?

Java 是否可以使用Hibernate 4从存储的T-SQL过程中获取两个输出参数?,java,sql-server,hibernate,tsql,Java,Sql Server,Hibernate,Tsql,我喜欢冬眠。 我需要使用Hibernate 4调用MS SQL 2008过程,并将结果打印到控制台 过程: 映射: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.

我喜欢冬眠。 我需要使用Hibernate 4调用MS SQL 2008过程,并将结果打印到控制台

过程:

映射:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <sql-query name="PRODUCT_CREATE" callable="true">
        <return-scalar column = "retVal" type="int"/>
        <return-scalar column = "cardId" type="int"/>
        <![CDATA[{CALL PRODUCT_CREATE (:ReturnValue, 
                                            :ProductID, 
                                            :PassID, 
                                            :Amount 
                                            )}]]>
  </sql-query>
</hibernate-mapping>
返回对象模型:

package app;

public class ProductModel {

    private int retVal;
    private int cardId;


    public ProductModel(int retVal, int cardId) {
        super();
        this.retVal = retVal;
        this.cardId = cardId;
    }


    public int getRetVal() {
        return retVal;
    }
    public void setRetVal(int retVal) {
        this.retVal = retVal;
    }
    public int getCardId() {
        return cardId;
    }
    public void setCardId(int cardId) {
        this.cardId = cardId;
    }

}
休眠配置:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="connection.url">jdbc:sqlserver://.......</property>
    <property name="connection.username">user</property>
    <property name="connection.password">pass</property>
    <property name="connection.pool_size">10</property>
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
    <property name="show_sql">true</property>
    <property name="hibernate.connection.autocommit">true</property>
    <!--<property name="current_session_context_class">thread</property> -->

    <mapping resource="app/PRODUCT_CREATE.hbm.xml"></mapping>

  </session-factory>
</hibernate-configuration> 

如何在控制台中修复异常并打印出过程的输出参数

这是整个stacktrace,还是只是它的顶部?胡乱猜测:您的ProductModel类没有默认的无参数构造函数,如果我是您,我会添加一个,因为这是将由框架自动创建和填充的类的一般规则。这只是我发现的,我没有理由相信,也没有任何证据证明这是这个问题的真正原因。这是没有hibernate日志的完整stacktrace。这是我的问题的简化示例。是的,很抱歉,找出如何修复异常的第一步是要知道是什么导致了异常,这里介绍的内容不可能做到这一点。当Hibernate处理结果集时,会发生一些问题。可能是错误的映射,可能是您没有显示的代码中的错误,无法真正了解。我专门准备了示例,并提供了异常的所有代码和文本。
package app;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {
    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;


    /**
     * Create hibernate configuration.
     */
    Configuration c = new Configuration().configure("/resources/hibernate.cfg.xml");


    public static SessionFactory createSessionFactory() {

        try
        {
        Configuration configuration = new Configuration();
        configuration.configure();
        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;

        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.out.print("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

}
package app;

public class ProductModel {

    private int retVal;
    private int cardId;


    public ProductModel(int retVal, int cardId) {
        super();
        this.retVal = retVal;
        this.cardId = cardId;
    }


    public int getRetVal() {
        return retVal;
    }
    public void setRetVal(int retVal) {
        this.retVal = retVal;
    }
    public int getCardId() {
        return cardId;
    }
    public void setCardId(int cardId) {
        this.cardId = cardId;
    }

}
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="connection.url">jdbc:sqlserver://.......</property>
    <property name="connection.username">user</property>
    <property name="connection.password">pass</property>
    <property name="connection.pool_size">10</property>
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
    <property name="show_sql">true</property>
    <property name="hibernate.connection.autocommit">true</property>
    <!--<property name="current_session_context_class">thread</property> -->

    <mapping resource="app/PRODUCT_CREATE.hbm.xml"></mapping>

  </session-factory>
</hibernate-configuration> 
<properties>
        <java.version>1.8</java.version>
        <jdk.version>1.8</jdk.version>
        <hibernate.version>4.3.8.Final</hibernate.version>
        <hibernate.jpa.version>1.0.0.Final</hibernate.jpa.version>
    </properties>

 <dependencies>

  <!--Hibernate-->
            <dependency>
                <groupId>org.hibernate.javax.persistence</groupId>
                <artifactId>hibernate-jpa-2.1-api</artifactId>
                <version>${hibernate.jpa.version}</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                <version>${hibernate.version}</version>
            </dependency>


   <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>sqljdbc4</artifactId>
        <version>4.0</version>
   </dependency>
    </dependencies>
 Exception in thread "main" java.lang.NullPointerException
    at org.hibernate.loader.Loader.processResultSet(Loader.java:950)
    at org.hibernate.loader.Loader.doQuery(Loader.java:921)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
    at org.hibernate.loader.Loader.doList(Loader.java:2554)
    at org.hibernate.loader.Loader.doList(Loader.java:2540)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)
    at org.hibernate.loader.Loader.list(Loader.java:2365)
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:353)
    at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1873)
    at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:311)
    at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:141)
    at app.App.createProduct(App.java:46)
    at app.App.main(App.java:16)