package com.dao.impl;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.commons.BaseHibernateDAO;
import com.dao.ITopicDao;
import com.orm.Topic;
import com.vo.QueryTopic;
/**
* A data access object (DAO) providing persistence and search support for Topic
* 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 com.orm.Topic
* @author MyEclipse Persistence Tools
*/
public class TopicDAO extends BaseHibernateDAO implements ITopicDao {
private static final Logger log = LoggerFactory.getLogger(TopicDAO.class);
// property constants
public static final String TITLE = "title";
public static final String CONTENT = "content";
public static final String FATHER = "father";
Session session = null;
Transaction tx = null;
public boolean save(Topic transientInstance) {
log.debug("saving Forum instance");
boolean flag = false;
try {
session = getSession();
tx = session.beginTransaction();
session.save(transientInstance);
log.debug("save successful");
tx.commit();
flag = true;
} catch (RuntimeException re) {
tx.rollback();
log.error("save failed", re);
throw re;
} finally {
session.close();
}
return flag;
}
public boolean delete(Topic persistentInstance) {
log.debug("deleting Forum instance");
boolean flag = false;
try {
session = getSession();
tx = session.beginTransaction();
session.delete(persistentInstance);
log.debug("save successful");
tx.commit();
flag = true;
} catch (RuntimeException re) {
tx.rollback();
log.error("save failed", re);
throw re;
} finally {
session.close();
}
return flag;
}
public Topic findById(java.lang.Long id) {
log.debug("getting Topic instance with id: " + id);
try {
Topic instance = (Topic) getSession().get("com.orm.Topic", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Topic instance) {
log.debug("finding Topic instance by example");
try {
List results = getSession().createCriteria("com.orm.Topic").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 Topic instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Topic 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 findByTitle(Object title) {
return findByProperty(TITLE, title);
}
public List findByContent(Object content) {
return findByProperty(CONTENT, content);
}
public List findByFather(Object father) {
return findByProperty(FATHER, father);
}
public List findAll() {
log.debug("finding all Topic instances");
try {
String queryString = "from Topic";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public Topic merge(Topic detachedInstance) {
log.debug("merging Topic instance");
try {
Topic result = (Topic) getSession().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(Topic instance) {
log.debug("attaching dirty Topic instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Topic instance) {
log.debug("attaching clean Topic instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public List<Topic> findByForumId(QueryTopic qt, int pageSize,
int pageIndex) {
String hql = "from Topic where users.username like :name and title like :title ";
if(qt.getForumId() != 0){
hql = hql + " and forum.id=:forumId";
}
session = getSession();
Query query = session.createQuery(hql);
query.setProperties(qt);
query.setFirstResult((pageIndex - 1)* pageSize);
query.setMaxResults(pageSize);
List<Topic> tlist = query.list();
return tlist;
}
public int getTotalPage(QueryTopic qt, int pageSize) {
String hql = "from Topic where users.username like :name and title like :title ";
if(qt.getForumId() != 0){
hql = hql + " and forum.id=:forumId";
}
session = getSession();
Query query = session.createQuery(hql);
query.setProperties(qt);
List<Topic> tlist = query.list();
int count = tlist.size();
int result = (count % pageSize == 0)?count / pageSize : count /pageSize + 1;
return result;
}
public boolean updateCount(Long id) {
boolean flag = false;
try {
String hql = "update Topic t set t.count = t.count + 1 where t.id =:id";
session = getSession();
tx = session.beginTransaction();
Query query = session.createQuery(hql);
query.setLong("id", id);
int key = query.executeUpdate();
if(key > 0)
flag = true;
tx.commit();
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return flag;
}
}
bbs.rar_BBS论坛_论坛发帖
版权申诉
150 浏览量
2022-09-24
05:20:46
上传
评论
收藏 6.3MB RAR 举报
林当时
- 粉丝: 103
- 资源: 1万+
最新资源
- git忽略文件的配置文件
- Koloro_v6.2.7.apk
- tensorflow-2.8.1-cp310-cp310-win-amd64.whl
- tensorflow-gpu-2.8.1-cp310-cp310-win-amd64.whl
- tensorflow-gpu-2.8.1-cp39-cp39-win-amd64.whl
- YOLOv5:基于深度学习的驾驶员分心驾驶行为(疲劳+危险行为)预警系统
- fast-backup 2.1.zip
- 室外lego-loam算法三维数据包
- mirthConnect h2database
- 基于Pyqt5+pyOpenGL实现WiFi信号强度三维模型python源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈