1. 云栖社区>
  2. 博客列表>
  3. 正文

herbnate session.createSQLQuery(sql) 和 session.createQuery(sql)使用

hjzgg 2016-04-28 16:40:09 浏览505 评论0

摘要: public class DistributeDao implements Serializable{ private SessionFactory sessionFactory; public Session getSession() { return sessionFactory.


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;
    }
}

版权声明:本文内容由互联网用户自发贡献,版权归作者所有,本社区不拥有所有权,也不承担相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:yqgroup@service.aliyun.com 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。

用云栖社区APP,舒服~

【云栖快讯】中办国办印发《推进互联网协议第六版(IPv6)规模部署行动计划》加快推进基于 IPv6 的下一代互联网规模部署,计划指出2025年末中国 IPv6 规模要达到世界第一,阿里云也第一时间宣布了将全面提供IPv6服务,那么在全面部署 IPV6 前,你需要了解都在这儿  详情请点击

网友评论

云数据库SQL Server,具有企业许可授权,权限更为开放,引擎功能更为强大。能完美支持Windows平台的.... 更多>

支持MySQL、SQL Server、PostgreSQL、MongoDB、Redis等关系型数据库和NoSQL... 更多>

云数据库HybridDB(ApsaraDB HybridDB)是一种在线MPP大规模并行处理数据仓库服务。云数据... 更多>

为您提供简单高效、处理能力可弹性伸缩的计算服务,帮助您快速构建更稳定、安全的应用,提升运维效率,降低 IT 成本... 更多>
订阅广场全新上线

订阅广场全新上线