Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 在hibernate 4中保存到数据库后未检索数据_Spring_Hibernate_Hibernate 4.x_Spring 4_Hibernate Session - Fatal编程技术网

Spring 在hibernate 4中保存到数据库后未检索数据

Spring 在hibernate 4中保存到数据库后未检索数据,spring,hibernate,hibernate-4.x,spring-4,hibernate-session,Spring,Hibernate,Hibernate 4.x,Spring 4,Hibernate Session,我正在使用Hibernate4和Spring4。我创建了自己的会话工厂,并使用Hibernate事务管理器。保存后检索数据时出现问题。 我使用ProcedureCall保存数据,并在每种方法中打开会话和关闭会话。有什么问题?如果我删除session.close(),那么它工作正常 public Map<String, Object> savePurchaseOrderInvoiceDetail(String dataString, String order_no,String eve

我正在使用Hibernate4和Spring4。我创建了自己的会话工厂,并使用Hibernate事务管理器。保存后检索数据时出现问题。 我使用ProcedureCall保存数据,并在每种方法中打开会话和关闭会话。有什么问题?如果我删除session.close(),那么它工作正常

public Map<String, Object> savePurchaseOrderInvoiceDetail(String dataString, String order_no,String event, HttpSession hs) throws SQLException, ParseException {
    HibernateTransactionManager htmLocal = (HibernateTransactionManager) hs.getAttribute("HibernateTransactionManager");
    Session session = htmLocal.getSessionFactory().openSession();
    Transaction tx = getTransaction(session);
    ProcedureCall qry = session.createStoredProcedureCall("purchase_order_invoice_api");
    qry.registerParameter(0, String.class, ParameterMode.IN).bindValue(event);
    qry.registerParameter(1, String.class, ParameterMode.IN).bindValue(dataString);
    qry.registerParameter(2, String.class, ParameterMode.OUT);
    qry.registerParameter(3, String.class, ParameterMode.OUT);
    qry.registerParameter(4, Integer.class, ParameterMode.OUT);
    qry.registerParameter(5, String.class, ParameterMode.OUT);
    ProcedureOutputs output = qry.getOutputs();

    String msg = (String) output.getOutputParameterValue(2);
    String voucheNo=(String) output.getOutputParameterValue(3);
    int invoiceId=(int) output.getOutputParameterValue(4);
    String status=(String) output.getOutputParameterValue(5);

    Map<String, Object>map=new HashMap<String, Object>();
    map.put("msg", msg);
    map.put("voucherNo", voucheNo);
    map.put("lastInvoiceId", invoiceId);
    map.put("status", status);

    tx.commit();

    session.close();
    return map;
}

public Map<String, Object> getInvoiceDetails(String invoicedId,HttpSession hs) throws Exception{
    HibernateTransactionManager htmLocal = (HibernateTransactionManager) hs.getAttribute("HibernateTransactionManager");
    Session session = htmLocal.getSessionFactory().openSession();

    final Map<String, Object>map=new HashMap<String, Object>();

    String company=(String) hs.getAttribute("company");
    int invoiceIdInt=Integer.valueOf(invoicedId);


    String qry = "select inv.*,get_supplier_name(inv.Company,inv.Identity) AS CUSTOMER_NAME from invoice_tab inv";
    Query query = session.createSQLQuery(qry).addEntity(Invoice.class);
    query.setCacheable(false);

    List<Invoice> invoiceList = query.list();

    for (int i = 0; i < invoiceList.size(); i++) {
        Invoice invoiceObj=invoiceList.get(i);

        //Business logic
    }

    session.close();
    return map;
}
公共映射savePurchaseOrderInvoiceDetail(字符串数据字符串、字符串顺序号、字符串事件、HttpSession hs)引发SQLException、ParseException{ HibernateTransactionManager htmLocal=(HibernateTransactionManager)hs.getAttribute(“HibernateTransactionManager”); Session Session=htmLocal.getSessionFactory().openSession(); 事务tx=getTransaction(会话); ProcedureCall qry=session.createStoredProcedureCall(“采购订单发票api”); qry.registerParameter(0,String.class,ParameterMode.IN).bindValue(事件); 注册表参数(1,String.class,ParameterMode.IN).bindValue(dataString); qry.registerParameter(2,String.class,ParameterMode.OUT); qry.registerParameter(3,String.class,ParameterMode.OUT); qry.registerParameter(4,Integer.class,ParameterMode.OUT); qry.registerParameter(5,String.class,ParameterMode.OUT); ProcedureOutputs output=qry.getOutputs(); String msg=(String)output.getOutputParameterValue(2); 字符串voucheNo=(字符串)output.getOutputParameterValue(3); int invoiceId=(int)output.getOutputParameterValue(4); 字符串状态=(字符串)输出。getOutputParameterValue(5); Mapmap=新的HashMap(); map.put(“msg”,msg); 地图放置(“沃切诺”,沃切诺); map.put(“lastInvoiceId”,invoiceId); 地图放置(“状态”,状态); tx.commit(); session.close(); 返回图; } 公共映射getInvoiceDetails(字符串invoicedId,HttpSession hs)引发异常{ HibernateTransactionManager htmLocal=(HibernateTransactionManager)hs.getAttribute(“HibernateTransactionManager”); Session Session=htmLocal.getSessionFactory().openSession(); final Mapmap=新HashMap(); 字符串公司=(字符串)hs.getAttribute(“公司”); int invoiceidit=Integer.valueOf(invoicedId); String qry=“选择存货*,从发票\u选项卡inv中获取供应商\u名称(存货公司、存货标识)作为客户\u名称”; Query Query=session.createSQLQuery(qry).addEntity(Invoice.class); query.setCacheable(false); List invoiceList=query.List(); 对于(int i=0;i您正在努力使用Spring,更不用说您的服务(或存储库)依赖于web应用程序这一事实了。这两件事都不好

@Transactional
注释添加到包含这些方法的类中,并启用注释驱动的事务管理。与其传递
HttpSession
,不如简单地注入依赖项,在本例中是
SessionFactory

不要自己创建会话使用当前会话,即
sessionFactory.getCurrentSession()
获取事务会话

@Service
@Transactional
public class YourService {

    private final SessionFactory sessionFactory;

    public YourService(SessionFactory sf) {
        this.sessionFactory=sf;
    }


    public Map<String, Object> savePurchaseOrderInvoiceDetail(String dataString, String order_no,String event) throws SQLException, ParseException {
        Session session = sessionFactory.getCurrentSession();
        ProcedureCall qry = session.createStoredProcedureCall("purchase_order_invoice_api");
        qry.registerParameter(0, String.class, ParameterMode.IN).bindValue(event);
        qry.registerParameter(1, String.class, ParameterMode.IN).bindValue(dataString);
        qry.registerParameter(2, String.class, ParameterMode.OUT);
        qry.registerParameter(3, String.class, ParameterMode.OUT);
        qry.registerParameter(4, Integer.class, ParameterMode.OUT);
        qry.registerParameter(5, String.class, ParameterMode.OUT);
        ProcedureOutputs output = qry.getOutputs();

        String msg = (String) output.getOutputParameterValue(2);
        String voucheNo=(String) output.getOutputParameterValue(3);
        int invoiceId=(int) output.getOutputParameterValue(4);
        String status=(String) output.getOutputParameterValue(5);

        Map<String, Object>map=new HashMap<String, Object>();
        map.put("msg", msg);
        map.put("voucherNo", voucheNo);
        map.put("lastInvoiceId", invoiceId);
        map.put("status", status);

        return map;
    }

    public Map<String, Object> getInvoiceDetails(int invoicedId, String company) throws Exception{
        Session session = sessionFactory.getCurrentSession();

        final Map<String, Object>map=new HashMap<String, Object>();

        String qry = "select inv.*,get_supplier_name(inv.Company,inv.Identity) AS CUSTOMER_NAME from invoice_tab inv";
        Query query = session.createSQLQuery(qry).addEntity(Invoice.class);
        query.setCacheable(false);

        List<Invoice> invoiceList = query.list();

        for (int i = 0; i < invoiceList.size(); i++) {
            Invoice invoiceObj=invoiceList.get(i);

            //Business logic
        }

        return map;
    }
}   
@服务
@交易的
公共服务{
私人最终会议工厂会议工厂;
公共服务(SessionFactory sf){
this.sessionFactory=sf;
}
公共映射savePurchaseOrderInvoiceDetail(字符串数据字符串、字符串顺序号、字符串事件)引发SQLException、ParseException{
Session Session=sessionFactory.getCurrentSession();
ProcedureCall qry=session.createStoredProcedureCall(“采购订单发票api”);
qry.registerParameter(0,String.class,ParameterMode.IN).bindValue(事件);
注册表参数(1,String.class,ParameterMode.IN).bindValue(dataString);
qry.registerParameter(2,String.class,ParameterMode.OUT);
qry.registerParameter(3,String.class,ParameterMode.OUT);
qry.registerParameter(4,Integer.class,ParameterMode.OUT);
qry.registerParameter(5,String.class,ParameterMode.OUT);
ProcedureOutputs output=qry.getOutputs();
String msg=(String)output.getOutputParameterValue(2);
字符串voucheNo=(字符串)output.getOutputParameterValue(3);
int invoiceId=(int)output.getOutputParameterValue(4);
字符串状态=(字符串)输出。getOutputParameterValue(5);
Mapmap=新的HashMap();
map.put(“msg”,msg);
地图放置(“沃切诺”,沃切诺);
map.put(“lastInvoiceId”,invoiceId);
地图放置(“状态”,状态);
返回图;
}
公共映射getInvoiceDetails(int invoicedId,字符串公司)引发异常{
Session Session=sessionFactory.getCurrentSession();
final Mapmap=新HashMap();
String qry=“选择存货*,从发票\u选项卡inv中获取供应商\u名称(存货公司、存货标识)作为客户\u名称”;
Query Query=session.createSQLQuery(qry).addEntity(Invoice.class);
query.setCacheable(false);
List invoiceList=query.List();
对于(int i=0;i
类似上面的东西


我真正不明白的是,为什么在创建自己的查询时使用hibernate,而不使用HQL或任何东西来获取实体。您使用hibernate的唯一目的是映射,这也可以通过普通SQL实现,在我的书中,将hibernate添加到您的项目中只是为了映射SQL结果有点过火。

对于初学者,请修复代码,您自己到底为什么要摆弄事务管理器。接下来,您将自己打开/创建hibernate会话,超出Spring及其事务管理的范围。您的代码也使用字符串concat f