📄 taddressbookdao.java
字号:
package hibernatemodel;
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
* TAddressbook 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 hibernatemodel.TAddressbook
* @author MyEclipse Persistence Tools
*/
public class TAddressbookDAO extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(TAddressbookDAO.class);
// property constants
public static final String NAME = "name";
public static final String PHONE = "phone";
public static final String ADDRESS = "address";
public static final String EMAIL = "email";
public static final String PROVINCE = "province";
public void save(TAddressbook transientInstance) {
log.debug("saving TAddressbook instance");
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
//hibernate插入操作
public void insert(TAddressbook transientInstance) {
log.debug("saving TAddressbook instance");
Transaction tx=getSession().beginTransaction();
try {
getSession().save(transientInstance);
tx.commit();
log.debug("save successful");
} catch (RuntimeException re) {
tx.rollback();
log.error("save failed", re);
throw re;
}
}
//修改
public void update(TAddressbook transientInstance) {
log.debug("saving TAddressbook instance");
Transaction tx=getSession().beginTransaction();
try {
getSession().merge(transientInstance);
//getSession().save(transientInstance);
tx.commit();
log.debug("save successful");
} catch (RuntimeException re) {
tx.rollback();
log.error("save failed", re);
throw re;
}
}
//hibernate查询操作
public List select(String strsql) {
try {
Query queryObject = getSession().createQuery(strsql);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
//删除操作
public void delete(TAddressbook persistentInstance) {
log.debug("deleting TAddressbook instance");
//hibernate查询操作
Transaction tx=getSession().beginTransaction();
//结束
try {
//hibernate查询操作
getSession().delete(persistentInstance);
tx.commit();
//
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public TAddressbook findById(java.lang.Integer id) {
log.debug("getting TAddressbook instance with id: " + id);
try {
TAddressbook instance = (TAddressbook) getSession().get(
"hibernatemodel.TAddressbook", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(TAddressbook instance) {
log.debug("finding TAddressbook instance by example");
try {
List results = getSession().createCriteria(
"hibernatemodel.TAddressbook")
.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 TAddressbook instance with property: "
+ propertyName + ", value: " + value);
try {
String queryString = "from TAddressbook 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 findByName(Object name) {
return findByProperty(NAME, name);
}
public List findByPhone(Object phone) {
return findByProperty(PHONE, phone);
}
public List findByAddress(Object address) {
return findByProperty(ADDRESS, address);
}
public List findByEmail(Object email) {
return findByProperty(EMAIL, email);
}
public List findAll() {
log.debug("finding all TAddressbook instances");
try {
String queryString = "from TAddressbook";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public TAddressbook merge(TAddressbook detachedInstance) {
log.debug("merging TAddressbook instance");
try {
TAddressbook result = (TAddressbook) getSession().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(TAddressbook instance) {
log.debug("attaching dirty TAddressbook instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(TAddressbook instance) {
log.debug("attaching clean TAddressbook 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 + -