package sunyang.functions.dao;
import java.util.List;
import javax.persistence.*;
import sunyang.functions.domain.Function;
import sunyang.persistence.EntityManagerHelper;
public class FunctionDAO implements IFunctionDAO {
// 声明静态常量
public static final String URL = "url";
public static final String FUNCTIONNAME = "functionname";
// 得到实体管理器
private EntityManager getEntityManager() {
return EntityManagerHelper.getEntityManager();
}
// 新增数据
public void save(Function entity) {
EntityManagerHelper.beginTransaction();
try {
getEntityManager().persist(entity);
EntityManagerHelper.commit();
} catch (RuntimeException re) {
EntityManagerHelper.rollback();
throw re;
}
}
// 删除数据
public void delete(Function entity) {
EntityManagerHelper.beginTransaction();
try {
entity = getEntityManager().getReference(Function.class,
entity.getId());
getEntityManager().remove(entity);
EntityManagerHelper.commit();
} catch (RuntimeException re) {
EntityManagerHelper.rollback();
throw re;
}
}
// 修改数据
public Function update(Function entity) {
EntityManagerHelper.beginTransaction();
try {
Function result = getEntityManager().merge(entity);
EntityManagerHelper.commit();
return result;
} catch (RuntimeException re) {
EntityManagerHelper.rollback();
throw re;
}
}
// 通过id查询数据
public Function findById(Integer id) {
try {
Function instance = getEntityManager().find(Function.class,
id);
return instance;
} catch (RuntimeException re) {
throw re;
}
}
// 通过表中一个字段查询数据
@SuppressWarnings("unchecked")
public List<Function> findByProperty(String propertyName,
final Object value) {
try {
final String queryString = "select model from Function model where model."
+ propertyName + "= :propertyValue";
Query query = getEntityManager().createQuery(queryString).setHint(
"toplink.refresh", true);
query.setParameter("propertyValue", value);
return query.getResultList();
} catch (RuntimeException re) {
throw re;
}
}
// 查询所有数据
@SuppressWarnings("unchecked")
public List<Function> findAll() {
try {
final String queryString = "select model from Function model";
Query query = getEntityManager().createQuery(queryString).setHint(
"toplink.refresh", true);
return query.getResultList();
} catch (RuntimeException re) {
throw re;
}
}
}
最近下载更多
hesu2020 LV1
2023年3月21日
微信网友_6044559891320832 LV2
2022年7月14日
2022982038 LV1
2022年1月27日
lizhiquan_119 LV2
2020年9月4日
281273445 LV3
2020年6月18日
qijie_nm LV1
2020年5月29日
luojiaqi LV1
2019年11月9日
11ssjhw LV1
2019年10月16日
A1231Q LV1
2019年6月18日
122582456 LV1
2019年6月17日

最近浏览
