public class DistributeDao implements Serializable{ private SessionFactory sessionFactory; public Session getSession() { return sessionFactory.getCurrentSession(); } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public Task getTask(Integer id) { Session session = this.getSession(); Transaction tran = session.beginTransaction(); List<Task> datas = session.createQuery("from TestMessage").list();//TestMessage是一個java類,這個類和某一個表對應! tran.commit(); return datas.get(id); } public void saveTask(Task task) { Session session = this.getSession(); Transaction tran = session.beginTransaction(); session.save(task); tran.commit(); } public List<Task> findAllTask() { Session session = this.getSession(); Transaction tran = session.beginTransaction(); List<Task> datas = session.createQuery("from Task").list(); tran.commit(); return datas; } public List<Task> findAllFinishedTask() { Session session = this.getSession(); Transaction tran = session.beginTransaction(); List<Task> datas = session.createQuery("from Task where state=2").list(); tran.commit(); return datas; } public List<Task> findUserTaskFinish(String developName){ Session session = this.getSession(); Transaction tran = session.beginTransaction(); String sql="select * from task where task.taskid in (select taskteam.taskid from taskteam where taskteam.developName=\'" + developName + "\') and task.state=2"; List<Task> datas = (List<Task>)session.createSQLQuery(sql).addEntity(Task.class).list(); tran.commit(); return datas; } public List<Task> findUserTaskNoParticipateTask(String developName){ Session session = this.getSession(); Transaction tran = session.beginTransaction(); String sql="select * from task where task.taskid not in (select taskteam.taskid from taskteam where developName=\'" + developName + "\')"; List<Task> datas = (List<Task>)session.createSQLQuery(sql).addEntity(Task.class).list();//必須有addEntity()方法,不然不能
//返回具體的類型,其實返回的是Object tran.commit(); return datas; } public List<Task> findUserTaskParticipating(String developName){ Session session = this.getSession(); Transaction tran = session.beginTransaction(); String sql="select * from task where task.taskid in (select taskteam.taskid from taskteam where developName=\'" + developName + "\') and task.state=1"; List<Task> datas = (List<Task>)session.createSQLQuery(sql).addEntity(Task.class).list(); tran.commit(); return datas; } public List<DevelopingParty> findTaskAllUser(int taskid){ Session session = this.getSession(); Transaction tran = session.beginTransaction(); String sql="select * from developingparty where developingparty.developName in (select taskteam.developingparty from taskteam where taskteam.taskid=" + taskid + ")"; List<DevelopingParty> datas = session.createSQLQuery(sql).addEntity(DevelopingParty.class).list(); tran.commit(); return datas; } }