Announcement Announcement Module
Collapse
No announcement yet.
using spring and hibernate for an SQL update query..?? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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?

  • #2
    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...

    Comment


    • #3
      Re: using spring and hibernate for an SQL update query..??

      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

      Comment


      • #4
        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.

        Comment


        • #5
          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);

          Comment

          Working...
          X