存储过程的NamednativeQuery返回null值

时间:2015-10-29 11:38:34

标签: hibernate

Product.java(实体类)

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedNativeQuery;
import javax.persistence.Table;
import com.opensymphony.xwork2.validator.annotations.*;

@NamedNativeQuery(
        name="callproductstoreprocedure" ,
        query="CALL getProducts()",
        resultClass=Product.class
    )
@Entity
@Table(name= "product",catalog="ProductInfo")
public class Product{

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private int id;
public String name;
private long price;
private int quantity;
private String description;

//setters and getters
}

Test.java

public class Test{
public List<Product> getProductDetails(){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    List<Product> products;

try {
        if (!session.getTransaction().isActive()){
            session.getTransaction().begin(); 

        }
        products=
  (List<Product>)session.getNamedQuery("callproductstoreprocedure").list();
     return products;   
    } catch (RuntimeException re) {
       return null;
    }
  }
 }

HibernateUtil.java

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

 private static SessionFactory buildSessionFactory() {
    try {
        // Create the SessionFactory from hibernate.cfg.xml
        return new AnnotationConfiguration().configure()
                .buildSessionFactory();
    } catch (Throwable ex) {
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
  }

  public static SessionFactory getSessionFactory() {
    return sessionFactory;
  }

 }

我有产品作为实体类。我正在使用javax.persistence的namednativequery注释。上面的存储过程执行bt返回null。为什么会这样? 我在控制台上收到此消息“ERROR:null,来自服务器的消息:”PROCEDURE  productinfo.getProducts无法返回给定上下文“”

中的结果集

0 个答案:

没有答案