简体   繁体   中英

Spring and Hibernate save not working

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

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>

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();    
        }

    }

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?

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();

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

尝试使用@Transactional方法,并将以下内容添加到XML:

<tx:annotation-driven/>

@Transaction you have give on method of service class and <tx:annotation-driven/> you have to give in applicaiton-context.xml file.

Hence, when any one call the service class's method, the transaciton will start by spring and it will handle up to commit and rollback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM