码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate反向工程生成DAO

时间:2014-11-12 22:44:49      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   java   


通过Hibernate反向工程生成个DAO:

package dao;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * Student entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see dao.Student
 * @author MyEclipse Persistence Tools
 */

public class StudentDAO extends BaseHibernateDAO {
    private static final Logger log = LoggerFactory.getLogger(StudentDAO.class);
    // property constants
    public static final String SNAME = "sname";
    public static final String EMAIL = "email";

    public void save(Student transientInstance) {
        log.debug("saving Student instance");
        try {
            getSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

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

    public Student findById(java.lang.Integer id) {
        log.debug("getting Student instance with id: " + id);
        try {
            Student instance = (Student) getSession().get("dao.Student", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List findByExample(Student instance) {
        log.debug("finding Student instance by example");
        try {
            List results = getSession().createCriteria("dao.Student")
                    .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;
        }
    }

    public List findByProperty(String propertyName, Object value) {
        log.debug("finding Student instance with property: " + propertyName
                + ", value: " + value);
        try {
            String queryString = "from Student as model where model."
                    + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List findBySname(Object sname) {
        return findByProperty(SNAME, sname);
    }

    public List findByEmail(Object email) {
        return findByProperty(EMAIL, email);
    }

    public List findAll() {
        log.debug("finding all Student instances");
        try {
            String queryString = "from Student";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

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

    public void attachDirty(Student instance) {
        log.debug("attaching dirty Student instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

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

以及相应的studentPO.java

bubuko.com,布布扣
package dao;

/**
 * Student entity. @author MyEclipse Persistence Tools
 */
public class Student extends AbstractStudent implements java.io.Serializable {

    // Constructors

    /** default constructor */
    public Student() {
    }

    /** full constructor */
    public Student(String sname, String email) {
        super(sname, email);
    }

}
View Code
bubuko.com,布布扣
package dao;

/**
 * AbstractStudent entity provides the base persistence definition of the
 * Student entity. @author MyEclipse Persistence Tools
 */

public abstract class AbstractStudent implements java.io.Serializable {

    // Fields

    private Integer sid;
    private String sname;
    private String email;

    // Constructors

    /** default constructor */
    public AbstractStudent() {
    }

    /** full constructor */
    public AbstractStudent(String sname, String email) {
        this.sname = sname;
        this.email = email;
    }

    // Property accessors

    public Integer getSid() {
        return this.sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public String getSname() {
        return this.sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}
View Code
bubuko.com,布布扣
package dao;

import org.hibernate.Session;


/**
 * Data access interface for domain model
 * @author MyEclipse Persistence Tools
 */
public interface IBaseHibernateDAO {
    public Session getSession();
}
View Code
bubuko.com,布布扣
package dao;

import hibernate.HibernateSessionFactory;
import org.hibernate.Session;


/**
 * Data access object (DAO) for domain model
 * @author MyEclipse Persistence Tools
 */
public class BaseHibernateDAO implements IBaseHibernateDAO {
    
    public Session getSession() {
        return HibernateSessionFactory.getSession();
    }
    
}
View Code

 

Hibernate反向工程生成DAO

标签:des   style   blog   http   io   color   ar   os   java   

原文地址:http://www.cnblogs.com/xfile/p/4093574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!