package db.hibernate;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import util.PageResult;
/**
* A data access object (DAO) providing persistence and search support for
* UserTb 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 db.hibernate.UserTb
* @author MyEclipse Persistence Tools
*/
public class UserTbDAO extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(UserTbDAO.class);
// property constants
public static final String USER_NAME = "userName";
public static final String USER_ADR = "userAdr";
public void save(UserTb transientInstance) {
log.debug("saving UserTb instance");
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(UserTb persistentInstance) {
log.debug("deleting UserTb instance");
try {
getSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public UserTb findById(java.lang.Integer id) {
log.debug("getting UserTb instance with id: " + id);
try {
UserTb instance = (UserTb) getSession().get("db.hibernate.UserTb",
id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(UserTb instance) {
log.debug("finding UserTb instance by example");
try {
List results = getSession().createCriteria("db.hibernate.UserTb")
.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 UserTb instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from UserTb 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 findByUserName(Object userName) {
return findByProperty(USER_NAME, userName);
}
public List findByUserAdr(Object userAdr) {
return findByProperty(USER_ADR, userAdr);
}
public List findAll() {
log.debug("finding all UserTb instances");
try {
String queryString = "from UserTb";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public UserTb merge(UserTb detachedInstance) {
log.debug("merging UserTb instance");
try {
UserTb result = (UserTb) getSession().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(UserTb instance) {
log.debug("attaching dirty UserTb instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(UserTb instance) {
log.debug("attaching clean UserTb instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public List findByExampleByPage(PageResult pageResult,UserTb user){
Criteria criteria=super.getSession().createCriteria(UserTb.class);
Example example=Example.create(user);
example.enableLike(MatchMode.ANYWHERE);
example.excludeZeroes();
criteria.add(example);
criteria.setProjection(Projections.rowCount());
int numOfRecords=((Integer)criteria.uniqueResult()).intValue();
pageResult.setNumOfRecords(numOfRecords);
criteria.setProjection(null);
int numOfRecordForStarting = pageResult.getNumOfRecordForStarting();
int sizeOfPage =pageResult.getSizeOfPage();
criteria.setFirstResult(numOfRecordForStarting);
criteria.setMaxResults(sizeOfPage);
List list=criteria.list();
return list;
}
public static void main(String[] args) {
PageResult pageResult=new PageResult();
pageResult.setCurrentPage(0);
pageResult.setSizeOfPage(3);
UserTb user=new UserTb();
List list=new UserTbDAO().findByExampleByPage(pageResult, user);
System.err.println(list.size());
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
UserTb utb=(UserTb) iterator.next();
System.out.println(utb.getUserName());
}
}
}
userTbDAO
最新推荐文章于 2019-04-28 11:35:55 发布
