-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How do I do an update?
PostPosted: Tue Feb 07, 2012 7:50 pm 
Newbie

Joined: Thu Jan 19, 2012 1:21 pm
Posts: 12
I was trying to follow the example in the document
http://docs.jboss.org/hibernate/orm/3.3 ... tch-direct

But get the following error in my server log:
SEVERE: org.hibernate.QueryException: query must begin with SELECT or FROM: update [update hibernate.TbUser u set u.vcUserName = :userName, u.vcUserPsw = :userPsw, u.vcUserFirstName = :firstName, u.vcUserLastName = :lastName, u.vcUserMiddleName = :middleName, u.vcEmailAddress = :email, u.vcPhoneNumber = :phone, u.dtTerminatedDate = :termDate, u.dtHireDate = :hireDate, u.dtUpdTime = :updTime, u.iUpdUserId = :userId where iUserId = :id]

This suggests I need to start the query with SELECT or FROM, however the example uses "Update" in the update query, so I do not understand what the problem is.

Can anyone show me to an example of how to do an Update???
The documentation apparently does not.
Thanks.

Here is the code I am trying to execute:
Code:
    public int updateUser( String userName, String passwd, String firstName, String middleName,
            String lastName, String email, String phone, Date termDate, Date hireDate, int id )
    {
        int status = 0;
        try
        {
            org.hibernate.Transaction tx = session.beginTransaction();
            String cmd = "update TbUser u set u.vcUserName = :userName, "+
                    "u.vcUserPsw = :userPsw, u.vcUserFirstName = :firstName, "+
                    "u.vcUserLastName = :lastName, u.vcUserMiddleName = :middleName, "+
                    "u.vcEmailAddress = :email, u.vcPhoneNumber = :phone, "+
                    "u.dtTerminatedDate = :termDate, u.dtHireDate = :hireDate, "+
                    "u.dtUpdTime = :updTime, u.iUpdUserId = :userId where iUserId = :id";
            Query q = session.createQuery( cmd );
            q.setString("userName", userName);
            q.setString("userPsw", passwd);
            q.setString("firstName", firstName);
            q.setString("middleName", middleName);
            q.setString("lastName", lastName);
            q.setString("email", email);
            q.setString("phone", phone);
            q.setDate("termDate", termDate);
            q.setDate("hireDate", hireDate);
            q.setDate("updTime", new Date());
            q.setInteger("id", id);
            status = q.executeUpdate();
            tx.commit();
        }
        catch( Exception e )
        {
            e.printStackTrace();
            status = -1;
        }
        return status;
    }



Top
 Profile  
 
 Post subject: Re: How do I do an update?
PostPosted: Wed Feb 08, 2012 2:28 am 
Newbie

Joined: Thu Feb 02, 2012 11:28 pm
Posts: 13
Location: shenzhen china
public int updateUser( String userName, String passwd, String firstName, String middleName, String lastName, String email, String phone, Date termDate, Date hireDate, int id )
{
int status = 0;
try
{
TbUser user = new TbUser();
user.setUserName(userName);
user.setPasswd(passwd);
......
......
org.hibernate.Transaction tx = session.beginTransaction();
try{
session.update(user,id);
tx.commit();
}catch(Exception e){
tx.rollback();
e.printStackTrace();
status = -1;
}finally{
try{
session.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
return status;
}


Top
 Profile  
 
 Post subject: Re: How do I do an update?
PostPosted: Wed Feb 08, 2012 1:55 pm 
Newbie

Joined: Thu Jan 19, 2012 1:21 pm
Posts: 12
Thanks for the feedback.
That you posted didn't work, but this variant did - I had toe fetch the row to update first.

Code:
    public int updateUser( String userName, String passwd,
            String firstName, String middleName, String lastName,
            String email, String phone, Date termDate,
            Date hireDate, int updtUserId, int id )
    {
        int status = 0;
        TbUser user = null;
        try
        {
            org.hibernate.Transaction tx = session.beginTransaction();
            user = (TbUser)session.get(TbUser.class, id);
            user.setVcUserName(userName);
            user.setVcUserPsw(passwd);
            user.setVcUserFirstName(firstName);
            user.setVcUserMiddleName(middleName);
            user.setVcUserLastName(lastName);
            user.setVcEmailAddress(email);
            user.setVcPhoneNumber(phone);
            user.setDtTerminatedDate(termDate);
            user.setDtHireDate(hireDate);
            user.setDtUpdTime( new Date() );
            user.setIupdUserId(updtUserId);
            try
            {
                 session.update(user);
                 tx.commit();
                status = 1;
            }
            catch(Exception e)
            {
                tx.rollback();
                e.printStackTrace();
                status = -1;
            }
//            finally
//            {
//                try
//                {
//                    session.close();
//                }
//                catch (Exception e)
//                {
//                    e.printStackTrace();
//                }
//            }
        }
        catch( Exception ex )
        {
            ex.printStackTrace();
            status = -1;
        }
        return status;
    }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.