📄 studentdao.java
字号:
package com.niit.dao.hibernate;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.niit.dao.IStudentDAO;
import com.niit.model.Student;
/**
* 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 com.niit.model.Student
* @author MyEclipse Persistence Tools
*/
public class StudentDAO extends HibernateDaoSupport implements IStudentDAO
{
private static final Log log = LogFactory.getLog(StudentDAO.class);
// property constants
public static final String SNAME = "sname";
public static final String CITY = "city";
public static final String AGE = "age";
protected void initDao()
{
// do nothing
}
/* (non-Javadoc)
* @see com.niit.dao.hibernate.IStudentDAO#save(com.niit.model.Student)
*/
public void save(Student transientInstance)
{
log.debug("saving Student instance");
try
{
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
}
catch (RuntimeException re)
{
log.error("save failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.niit.dao.hibernate.IStudentDAO#delete(com.niit.model.Student)
*/
public void delete(Student persistentInstance)
{
log.debug("deleting Student instance");
try
{
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
}
catch (RuntimeException re)
{
log.error("delete failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.niit.dao.hibernate.IStudentDAO#findById(java.lang.Integer)
*/
public Student findById(java.lang.Integer id)
{
log.debug("getting Student instance with id: " + id);
try
{
Student instance = (Student) getHibernateTemplate().get(
"com.niit.model.Student", id);
return instance;
}
catch (RuntimeException re)
{
log.error("get failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.niit.dao.hibernate.IStudentDAO#findByExample(com.niit.model.Student)
*/
public List findByExample(Student instance)
{
log.debug("finding Student instance by example");
try
{
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
}
catch (RuntimeException re)
{
log.error("find by example failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.niit.dao.hibernate.IStudentDAO#findByProperty(java.lang.String, java.lang.Object)
*/
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 + "= ?";
return getHibernateTemplate().find(queryString, value);
}
catch (RuntimeException re)
{
log.error("find by property name failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.niit.dao.hibernate.IStudentDAO#findBySname(java.lang.Object)
*/
public List findBySname(Object sname)
{
return findByProperty(SNAME, sname);
}
/* (non-Javadoc)
* @see com.niit.dao.hibernate.IStudentDAO#findByCity(java.lang.Object)
*/
public List findByCity(Object city)
{
return findByProperty(CITY, city);
}
/* (non-Javadoc)
* @see com.niit.dao.hibernate.IStudentDAO#findByAge(java.lang.Object)
*/
public List findByAge(Object age)
{
return findByProperty(AGE, age);
}
/* (non-Javadoc)
* @see com.niit.dao.hibernate.IStudentDAO#findAll()
*/
public List findAll()
{
log.debug("finding all Student instances");
try
{
String queryString = "from Student";
return getHibernateTemplate().find(queryString);
}
catch (RuntimeException re)
{
log.error("find all failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.niit.dao.hibernate.IStudentDAO#merge(com.niit.model.Student)
*/
public Student merge(Student detachedInstance)
{
log.debug("merging Student instance");
try
{
Student result = (Student) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
}
catch (RuntimeException re)
{
log.error("merge failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.niit.dao.hibernate.IStudentDAO#attachDirty(com.niit.model.Student)
*/
public void attachDirty(Student instance)
{
log.debug("attaching dirty Student instance");
try
{
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
}
catch (RuntimeException re)
{
log.error("attach failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.niit.dao.hibernate.IStudentDAO#attachClean(com.niit.model.Student)
*/
public void attachClean(Student instance)
{
log.debug("attaching clean Student instance");
try
{
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
}
catch (RuntimeException re)
{
log.error("attach failed", re);
throw re;
}
}
public static IStudentDAO getFromApplicationContext(ApplicationContext ctx)
{
return (IStudentDAO) ctx.getBean("StudentDAO");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -