我是hibernate的新手。 我需要使用Hibernate 4调用MS SQL 2008程序并将结果打印到控制台。
PROC:
CREATE PROCEDURE [dbo].[PRODUCT_CREATE](
@ReturnValue INT output,
@ProductID INT output,
@PassID numeric(18,0),
@Amount numeric(18,2))
as
BEGIN
print 'PassID = ' + convert(varchar, @PassID);
print 'Amount = ' + convert(varchar, @Amount);
select @ReturnValue = 999999,
@ProductID = 777777;
END;
映射:
<?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;
import java.math.BigInteger;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
public class App {
public static void main(String[] args) {
App.createProduct();
}
public static BigInteger createProduct()
{
/**
* Open session and begin database transaction for database operation.
*/
SessionFactory sf = HibernateUtil.createSessionFactory();
Session session = sf.openSession();
Query qr = session.getNamedQuery("PRODUCT_CREATE");
qr.setParameter("ReturnValue", 0);
qr.setParameter("ProductID", 0);
qr.setParameter("PassID", new BigInteger("1000999"));
qr.setParameter("Amount", new BigInteger("1000001"));
@SuppressWarnings("unchecked")
/*The problem is here*/
List<ProductModel> list = qr.setResultTransformer(Transformers.aliasToBean(ProductModel.class)).list();
for(int i=0; i<list.size(); i++){
ProductModel prd = (ProductModel)list.get(i);
System.out.println(String.valueOf(prd.getCardId()));
}
session.getTransaction().commit();
session.close();
return null;
}
}
连接设置:
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;
}
}
Hibernate conf:
<?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>
POM:
<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)
如何在控制台中修复异常并打印出程序的输出参数?