Results 1 to 5 of 5

Thread: using spring and hibernate for an SQL update query..??

  1. #1
    Join Date
    Aug 2004
    Location
    Tacoma, WA USA
    Posts
    49

    Default using spring and hibernate for an SQL update query..??

    I'm using spring and hibernate and I need to do a simple update:

    Code:
    UPDATE ASYNCDATA SET MESSAGESTATUS='PROCESSING', BROADCASTSTATUS='', CONVERSIONREQUIRED='TRUE' where SSN='000552111';
    I can't figure out how to pull this off with getHibernateTemplate calls. I've looked at the API docs, refs, etc.... Suggestions? Examples?
    ElPapa

    The delusion that people care about what I think:
    http://www.codethought.com/blog

  2. #2
    Join Date
    Jul 2005
    Location
    Lancashire, England
    Posts
    18

    Default

    As I understand it (although a quick read of my posts will tell you that isn't too well), HQL is simply for retrieval of objects, and not for updating them.
    You could retrieve the POJOs and iterate through them, updating them as you went, or it may be possible to run the SQL you want through Hibernate in some way (although I'm afraid I don't know how, if you can).
    But HQL is simply for retreival (or that's what my book said...).
    doug.

    PS -- hope I haven't misunderstood what you were asking...
    Common sense is what tells you the world is flat.

  3. #3
    Join Date
    Sep 2005
    Location
    Vienna
    Posts
    44

    Default Re: using spring and hibernate for an SQL update query..??

    Quote Originally Posted by ElPapa
    I'm using spring and hibernate and I need to do a simple update...
    I must first correct biot023: it is possible to issue updates in HQL - since Hibernate 3.0 at least.

    Back to the actual question - you'll have to write a "beast" like:
    Code:
    getHibernateTemplate().execute(new HibernateCallback() {
    	public Object doInHibernate(Session session) throws HibernateException, SQLException {
    		SQLQuery query = session
    				.createSQLQuery("UPDATE ASYNCDATA SET MESSAGESTATUS='PROCESSING', BROADCASTSTATUS='', CONVERSIONREQUIRED='TRUE' "
    						+ "where SSN='000552111'");
    		query.executeUpdate();
    		return null;
    	};
    });
    Note that variations on this are possible (see the Hibernate documentation on Query and SQLQuery) and that what I wrote above may not work as is - but this is the general intention.

    Not knowing enough about your POJOs I can't really offer you a sensible version of this using HQL and Query.

    If you have a concrete problem, simply post again...

    Erik

  4. #4
    Join Date
    Aug 2004
    Location
    Tacoma, WA USA
    Posts
    49

    Default

    Just to share what the end result of this message was..

    - First... the query as EricFK coded it doesn't work, because updates can only be done via HQL. At least that's what Hibernate says.

    - Even after configuring the query to use HQL it *still* won't work in my environment because I'm using WebLogic 8.1 and there's a ANTLR conflict that crashes WL 8 when I try.

    - The Hibernate forum suggests replacing the ANTLR in WLS 8 with a 'patched' version that should fix the problem. However, the project I'm working on is for a major Telecom provider. You can't just "switch out" a core library. That can impact other systems.

    So beware of using the update capabilities in HQL if you need to deploy to WebLogic 8.1. You could be in for a world of hurt.
    ElPapa

    The delusion that people care about what I think:
    http://www.codethought.com/blog

  5. #5

    Default

    Quote Originally Posted by ElPapa View Post
    I'm using spring and hibernate and I need to do a simple update:

    Code:
    UPDATE ASYNCDATA SET MESSAGESTATUS='PROCESSING', BROADCASTSTATUS='', CONVERSIONREQUIRED='TRUE' where SSN='000552111';
    I can't figure out how to pull this off with getHibernateTemplate calls. I've looked at the API docs, refs, etc.... Suggestions? Examples?
    hey
    we can use this piece of code:
    String query="UPDATE User set userPassword='"+password+"'where userId="+userid;
    //this.getHibernateTemplate().update(query);
    or for directly entities updation
    this.getHibernateTemplate().saveOrUpdate(userEntit y);

Similar Threads

  1. Spring Query API
    By scottr in forum Data
    Replies: 8
    Last Post: Jul 28th, 2010, 02:14 PM
  2. Replies: 3
    Last Post: Sep 7th, 2006, 12:49 PM
  3. Replies: 1
    Last Post: Aug 9th, 2005, 02:46 AM
  4. Spring + Hibernate ORA-00936: missing expression
    By Hugh_la_Main in forum Data
    Replies: 1
    Last Post: Jun 28th, 2005, 08:48 AM
  5. Spring code remarks
    By Alarmnummer in forum Architecture
    Replies: 18
    Last Post: Apr 7th, 2005, 07:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •