📄 guestbookdao.java
字号:
package org.openblog.hibernate;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
/**
* A data access object (DAO) providing persistence and search support for
* Guestbook 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 org.openblog.hibernate.Guestbook
* @author MyEclipse Persistence Tools
*/
public class GuestbookDAO extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(GuestbookDAO.class);
// property constants
public static final String GUEST_NAME = "guestName";
public static final String GUEST_URL = "guestUrl";
public static final String GUEST_QQ = "guestQq";
public static final String GUEST_EMAIL = "guestEmail";
public static final String GUEST_CONTENT = "guestContent";
public static final String GUEST_STATUS = "guestStatus";
public void save(Guestbook transientInstance) {
Transaction trans = null;
log.debug("saving Guestbook instance");
try {
trans = getSession().beginTransaction();
getSession().save(transientInstance);
getSession().flush();
trans.commit();
log.debug("save successful");
} catch (RuntimeException re) {
if(trans!=null) trans.rollback();
log.error("save failed", re);
throw re;
}
}
public void delete(Guestbook persistentInstance) {
Transaction trans = null;
log.debug("deleting Guestbook instance");
try {
trans = getSession().beginTransaction();
getSession().delete(persistentInstance);
getSession().flush();
trans.commit();
log.debug("delete successful");
} catch (RuntimeException re) {
if(trans!=null) trans.rollback();
log.error("delete failed", re);
throw re;
}
}
public void modify(Guestbook persistentInstance) {
Transaction trans = null;
log.debug("modify Guestbook instance");
try {
trans = getSession().beginTransaction();
getSession().update(persistentInstance);
getSession().flush();
trans.commit();
log.debug("modify successful");
} catch (RuntimeException re) {
if(trans!=null) trans.rollback();
log.error("modify failed", re);
throw re;
}
}
public Guestbook findById(java.lang.Integer id) {
log.debug("getting Guestbook instance with id: " + id);
try {
Guestbook instance = (Guestbook) getSession().get(
"org.openblog.hibernate.Guestbook", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Guestbook instance) {
log.debug("finding Guestbook instance by example");
try {
List results = getSession().createCriteria(
"org.openblog.hibernate.Guestbook").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 Guestbook instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Guestbook 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 findByGuestName(Object guestName) {
return findByProperty(GUEST_NAME, guestName);
}
public List findByGuestUrl(Object guestUrl) {
return findByProperty(GUEST_URL, guestUrl);
}
public List findByGuestQq(Object guestQq) {
return findByProperty(GUEST_QQ, guestQq);
}
public List findByGuestEmail(Object guestEmail) {
return findByProperty(GUEST_EMAIL, guestEmail);
}
public List findByGuestContent(Object guestContent) {
return findByProperty(GUEST_CONTENT, guestContent);
}
public List findByGuestStatus(Object guestStatus) {
return findByProperty(GUEST_STATUS, guestStatus);
}
public List findAll() {
log.debug("finding all Guestbook instances");
try {
String queryString = "from Guestbook";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public List findbyPage(int PageNo , int PageSize) {
log.debug("finding all Guestbook instances");
try {
String queryString = "from Guestbook as model order by model.guestId desc";
Query queryObject = getSession().createQuery(queryString);
queryObject.setFirstResult((PageNo-1)*PageSize);
queryObject.setMaxResults(PageSize);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public Object countGuest() {
log.debug("finding all Guestbook instances");
try {
String queryString = "select count(*) from Guestbook";
Query queryObject = getSession().createQuery(queryString);
return queryObject.uniqueResult();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public List findLatest(int num) {
log.debug("finding all Guestbook instances");
try {
String queryString = "from Guestbook as a order by a.guestId desc";
Query queryObject = getSession().createQuery(queryString);
queryObject.setMaxResults(num);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public Guestbook merge(Guestbook detachedInstance) {
log.debug("merging Guestbook instance");
try {
Guestbook result = (Guestbook) getSession().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(Guestbook instance) {
log.debug("attaching dirty Guestbook instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Guestbook instance) {
log.debug("attaching clean Guestbook instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -