Spring和Hibernate保存不起作用 [英] Spring and Hibernate save not working

查看:111
本文介绍了Spring和Hibernate保存不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Hibernate和Spring 3.0我试图将值保存到数据库中,但是当我看到控制台时,唯一的选择查询显示插入或更新未显示,并且保存不起作用

I am using Hibernate and Spring 3.0 i am trying to save the value into database but when i see a console the only select query is showing insert or update is not showing and save is not working

我创建了一个sessionFactory bean,并将它注入到Impl中。

I created a sessionFactory bean and inject it into Impl

 <bean id="GetStartedDAOBean" class="com.sample.dao.impl.GetStartedDAOImpl" >
            <property name="sessionfactory" ref="sessionFactory">
            </property>
    </bean

<bean id="GetStartedActionBean" class="com.sample.action.GetStartedAction">
        <property name="getStartedDAOImpl" ref="GetStartedDAOBean"></property>
        <property name="industryDAOImpl" ref="IndustryDAOBean"></property>
        <property name="stateDAOImpl" ref="stateDAOBean"></property>
    </bean>

在impl中,我有

In impl i have

private SessionFactory sessionfactory;

      public void setSessionfactory(SessionFactory sessionfactory) {
        this.sessionfactory = sessionfactory;
      }


    public void save(Customer customer)throws IllegalStateException,SystemException{

        try {
            sessionfactory.openSession().saveOrUpdate(customer);
        }
        catch(Exception e){
            e.printStackTrace();    
        }

    }

当我调试时, sessionFactory但它不保存任何值。也不显示任何插入的查询。没有错误。

when i debug there is value in sessionFactory but it does not save any value. and also does not show any inserted query. There is no error.

任何人都可以帮到我吗?

Any one can help me?

推荐答案

你打开你的会话(内存中)并保存一些内容,但只有在 flush()时,会话才会保存在数据库中。做一个

You open your session (in-memory) and save something onto it, but the session saves in the database only when you flush(). Do a

Session session = sessionfactory.openSession();
session.saveOrUpdate(customer);
session.flush();

另一种方法是提交事务,因此Hibernate会自动调用 flush ()

Another way is to commit the transaction, and thus Hibernate will automatically call flush().

这篇关于Spring和Hibernate保存不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆