userhome hibernate session saveorupdate 与事务的关系

package jiajiao.qiantai;
// Generated 2018-5-16 11:51:31 by Hibernate Tools 3.5.0.Final

import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Example;

/**
 * Home object for domain model class User.
 * @see jiajiao.qiantai.User
 * @author Hibernate Tools
 */
public class UserHome {

    private static final Log log = LogFactory.getLog(UserHome.class);

    private final SessionFactory sessionFactory = getSessionFactory();



    protected SessionFactory getSessionFactory() {
        try {
            return (SessionFactory) new Configuration().configure().buildSessionFactory();
        } catch (Exception e) {
            log.error("Could not locate SessionFactory in JNDI", e);
            throw new IllegalStateException("Could not locate SessionFactory in JNDI");
        }
    }

    public void persist(User transientInstance) {
        log.debug("persisting User instance");
        try {
            //sessionFactory.getCurrentSession().persist(transientInstance);
            Session session = sessionFactory.openSession();
            session.getSession().persist(transientInstance);
            //getSessionFactory().getCurrentSession().persist(transientInstance);
            log.debug("persist successful");
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }


    public void attachDirty(User instance) {
        log.debug("attaching dirty User instance");
        try {
            System.out.println(instance+"--->实例");
            Session session = sessionFactory.openSession();
            session.getSession().saveOrUpdate(instance);
            System.out.println("attachDirty 被执行了");
            //sessionFactory.getCurrentSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(User instance) {
        log.debug("attaching clean User instance");
        try {
            sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void delete(User persistentInstance) {
        log.debug("deleting User instance");
        try {
            sessionFactory.getCurrentSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public User merge(User detachedInstance) {
        log.debug("merging User instance");
        try {
            User result = (User) sessionFactory.getCurrentSession().merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public User findById(int id) {
        log.debug("getting User instance with id: " + id);
        try {
            User instance = (User) sessionFactory.getCurrentSession().get("jiajiao.qiantai.User", id);
            if (instance == null) {
                log.debug("get successful, no instance found");
            } else {
                log.debug("get successful, instance found");
            }
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }


    public List findByExample(User instance) {
        log.debug("finding User instance by example");
        try {

            Session session = sessionFactory.openSession();

            List results = session.getSession().createCriteria("jiajiao.qiantai.User")
                    .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }
}
package jiajiao.qiantai;
// Generated 2018-5-16 11:51:31 by Hibernate Tools 3.5.0.Final

import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Example;

/**
 * Home object for domain model class User.
 * @see jiajiao.qiantai.User
 * @author Hibernate Tools
 */
public class UserHome {

    private static final Log log = LogFactory.getLog(UserHome.class);

    private final SessionFactory sessionFactory = getSessionFactory();



    protected SessionFactory getSessionFactory() {
        try {
            return (SessionFactory) new Configuration().configure().buildSessionFactory();
        } catch (Exception e) {
            log.error("Could not locate SessionFactory in JNDI", e);
            throw new IllegalStateException("Could not locate SessionFactory in JNDI");
        }
    }

    public void persist(User transientInstance) {
        log.debug("persisting User instance");
        try {
            //sessionFactory.getCurrentSession().persist(transientInstance);
            Session session = sessionFactory.openSession();
            session.getSession().persist(transientInstance);
            //getSessionFactory().getCurrentSession().persist(transientInstance);
            log.debug("persist successful");
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }


    public void attachDirty(User instance) {
        log.debug("attaching dirty User instance");
        try {
            //System.out.println(instance+"--->实例");
            Session session = sessionFactory.openSession();
            //开启事务
            session.beginTransaction();
            //session执行
            session.saveOrUpdate(instance);
            //事务提交
            session.getTransaction().commit();


            //session.getSession().saveOrUpdate(instance);
            //System.out.println("attachDirty 被执行了");
            //sessionFactory.getCurrentSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(User instance) {
        log.debug("attaching clean User instance");
        try {
            sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void delete(User persistentInstance) {
        log.debug("deleting User instance");
        try {
            sessionFactory.getCurrentSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public User merge(User detachedInstance) {
        log.debug("merging User instance");
        try {
            User result = (User) sessionFactory.getCurrentSession().merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public User findById(int id) {
        log.debug("getting User instance with id: " + id);
        try {
            User instance = (User) sessionFactory.getCurrentSession().get("jiajiao.qiantai.User", id);
            if (instance == null) {
                log.debug("get successful, no instance found");
            } else {
                log.debug("get successful, instance found");
            }
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }


    public List findByExample(User instance) {
        log.debug("finding User instance by example");
        try {

            Session session = sessionFactory.openSession();

            List results = session.getSession().createCriteria("jiajiao.qiantai.User")
                    .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }
}

1、在对数据进行查找时,即使你失败并不影响数据库中的数据;在对数据库进行添加时,那就不同了,当你添加数据失败时,可能会在数据库中留下垃圾数据。这是我们就要用到事务,事务他提交失败时,他就会自动回滚。这样就不影响数据库了。
2、在对hibernate框架使用时,你不重新开启事务的话,你是不能对数据进行增删改的,因为事务默认为是失败的,也就是数他一直处于回滚状态。所以你每次操作都是不成功的!

猜你喜欢

转载自blog.csdn.net/immenselee/article/details/80599519