package com.beehive.db.dao.impl.forum;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import com.beehive.db.dao.forum.TopicDAO;
import com.beehive.db.entity.forum.Topic;
@Component("topicDAO")
public class TopicDAOHibernateImpl implements TopicDAO {
private SessionFactory sessionFactory;
private Session getSession(){
return sessionFactory.getCurrentSession();
}
public void delete(Topic topic) {
getSession().delete(topic);
}
public void delete(long id) {
delete(getByID(id));
}
public Topic getByID(long id) {
return (Topic)getSession().get(Topic.class, id);
}
@SuppressWarnings("unchecked")
public List<Topic> getByTitle(String title) {
String hql = "from Topic where title = '" + title + "'";
Query query = getSession().createQuery(hql);
return query.list();
}
@SuppressWarnings("unchecked")
public List<Topic> getMultiple(int first, int max) {
String hql = "from Topic";
Query query = getSession().createQuery(hql);
query.setFirstResult(first);
query.setMaxResults(max);
return query.list();
}
public void save(Topic topic) {
getSession().save(topic);
}
public void update(Topic topic) {
getSession().update(topic);
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
|