package org.spring.dao;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.hibernate.bean.Users;
/**
* Data access object (DAO) for domain model class Users.
* @see com.hibernate.bean.Users
* @author MyEclipse - Hibernate Tools
*/
public class UsersDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(UsersDAO.class);
//property constants
public static final String SNAME = "sname";
public static final String SDEPARTMENT = "sdepartment";
public static final String SLOGIN_NAME = "sloginName";
public static final String SPASS_WORD = "spassWord";
public static final String ICOMPETENCE = "icompetence";
protected void initDao() {
//do nothing
}
public void save(Users transientInstance) {
log.debug("saving Users instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void update(Users user)
{
log.debug("update user");
try
{
getHibernateTemplate().update(user);
log.debug("update user successful");
}
catch(RuntimeException re)
{
log.debug("update user failed",re);
throw re;
}
}
public void delete(Users persistentInstance) {
log.debug("deleting Users instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public List findAllUsers()
{
log.debug("getting All Users instance");
List list = null;
try
{
// System.out.println("UsersDAO中findAllUsers");
list = getHibernateTemplate().find("from Users");
log.debug("get All Users successful");
System.out.println(list.size());
return list;
}
catch(RuntimeException re)
{
log.error("get all Users failed",re);
throw re;
}
}
public Users findById( java.lang.Integer id) {
log.debug("getting Users instance with id: " + id);
try {
Users instance = (Users) getHibernateTemplate()
.get("com.hibernate.bean.Users", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Users instance) {
log.debug("finding Users 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;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding Users instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Users as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findBySname(Object sname) {
return findByProperty(SNAME, sname);
}
public List findBySdepartment(Object sdepartment) {
return findByProperty(SDEPARTMENT, sdepartment);
}
public List findBySloginName(Object sloginName) {
return findByProperty(SLOGIN_NAME, sloginName);
}
public List findBySpassWord(Object spassWord) {
return findByProperty(SPASS_WORD, spassWord);
}
public List findByIcompetence(Object icompetence) {
return findByProperty(ICOMPETENCE, icompetence);
}
public Users merge(Users detachedInstance) {
log.debug("merging Users instance");
try {
Users result = (Users) getHibernateTemplate()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(Users instance) {
log.debug("attaching dirty Users instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Users instance) {
log.debug("attaching clean Users instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static UsersDAO getFromApplicationContext(ApplicationContext ctx) {
return (UsersDAO) ctx.getBean("UsersDAO");
}
public List findAll()
{
String sql="FROM Users";
return getHibernateTemplate().find(sql);
}
public List findAllByPage( final int pageNow, final int pageSize)
{
List list = (List) getHibernateTemplate().execute(
new HibernateCallback()
{
int size=pageNow*pageSize-pageSize;
public Object doInHibernate(Session session)throws HibernateException
{
Query q =session.createQuery("from User order by id");
q.setFirstResult(size);
q.setMaxResults(pageSize);
List cats = q.list();
return cats;
}
});//内部类,返回一个List
return list;
}
}
评论0
最新资源