如何在Hibernate中执行一系列选择查询?

时间:2014-09-02 07:00:08

标签: java mysql hibernate hql

我是Hql的新手。我在选择查询中遇到问题。我实际上正在进行组重命名选项。首先,我正在寻找pericular Id。接下来,如果没有新组名的组名,那么我正在更新旧组名。我的方法是这样的。

private void renameGroup(String ogname, String gname , String clientid) {
    //Here   ogname="group7"
    //       gname="group8"

    System.out.println("Old group name2="+ogname);
    Session hSession = HibernateSession.getHibernateSession();
    Transaction transaction = hSession.beginTransaction();


    String hql= "select groupname from GroupDetails where clientid=:clientid  and groupname=:oldGroupName";
    Query query = hSession.createQuery(hql);
    query.setParameter("clientid", Integer.valueOf(clientid));
    query.setParameter("oldGroupName",ogname );
    List<Object[]> groupList = (List<Object[]>) query.list(); // This has groupname [group7]

    if(!(groupList.isEmpty()))
    {
        Session hSession2 = HibernateSession.getHibernateSession();
        Transaction transaction2 = hSession.beginTransaction();
        String hql2= "select groupname from GroupDetails where clientid =:clientid  and groupname=:newGname";
        Query query2 = hSession2.createQuery(hql2);
        query2.setParameter("clientid", Integer.valueOf(clientid));
        query2.setParameter("newGname",gname );
        List<Object[]> groupList2 = (List<Object[]>) query2.list();//Here it has to be empty so that I can update but it also has [group7]

        if(groupList2.isEmpty())
        {// So it never come to this block
            String hql3="update GroupDetails set groupname=:newGroupName where clientid=:clientid  and groupname=:ogname";
            Query query3 = hSession.createQuery(hql2);
            query3.setParameter("newGroupName", gname);
            query3.setParameter("clientid",Integer.valueOf(clientid));
            query3.setParameter("ogname",ogname);
            int res = query3.executeUpdate();
            System.out.println("Command successfully executed....");
            System.out.println("Numer of records effected due to update query"+res);
        }

    }


} 

观察我的评论,以便更好地了解我的问题。我认为问题在于会话和交易。但我没有得到。请帮我解决这个问题。谢谢

0 个答案:

没有答案