0

Hibernate query.executeUpdate() is not working..

Here is the code for updating

   public static void expDue(){
   Session session=HibernateUtil.getSessionFactory().openSession();
    java.util.Date utilDate=new java.util.Date();
    java.sql.Date sqldate=new java.sql.Date(utilDate.getTime());
    Format formatter = new SimpleDateFormat("yyyy-MM-dd");
    String a= formatter.format(sqldate);      
    boolean b=false;
    if(b==false){
    Query query = session.createQuery(" update Issue set dueStatus = 'true' where returnDate='"+a+"'");
    int result = query.executeUpdate();
    System.out.println(query.executeUpdate()+"Rows affected: " + result);
    b=true;
    }

Here, printing the result shows correct value, but no change in database.

And hibernate code

<hibernate-configuration>
  <session-factory>
    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.HSQLDialect</property> 
 
    <!-- Database connection settings --> 
    <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="hibernate.connection.url">jdbc:hsqldb:db/hsql/library;shutdown=true</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="hibernate.connection.password">sa</property>
        <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
    
 
    <!-- JDBC connection pool (use the built-in one) -->
    <property name="connection.pool_size">1</property> 
 
    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>
 
    <!-- Disable the second-level cache  --> 
    <property
     name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
 
    <!-- disable batching so HSQLDB will propagate errors correctly. -->
    <property name="jdbc.batch_size">0</property> 
 
    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property> 
 
    <!-- List all the mapping documents we're using --> 
    <mapping class="com.habitz.librarymanagement.domain.Admin" />
        <mapping class="com.librarymanagement.domain.Book" />
        <mapping class="com.librarymanagement.domain.Category" />
        <mapping class="com.librarymanagement.domain.Group" />
        <mapping class="com.librarymanagement.domain.Issue" />
        <mapping class="com.librarymanagement.domain.Member" />
  </session-factory>
</hibernate-configuration>

In console printing the result shows correct values. But the database shows no change...

If you know about this please share answers here...

UPDATE

Transaction tx = null;
        tx = session.beginTransaction();
            Query query = session
                    .createQuery(" update Issue set dueStatus = 'true' where returnDate='"
                            + a + "'");
        
            int result = query.executeUpdate();
            System.out.println(query.executeUpdate() + "Rows affected: "
                    + result);  
            tx.commit();
6
  • you don't commit transaction in database. Commented Feb 28, 2014 at 7:00
  • @user1516873 transaction commit like this Transaction tx = null; tx = session.beginTransaction(); tx.commit(); Commented Feb 28, 2014 at 7:03
  • @user1516873 i update but it's still not working... Commented Feb 28, 2014 at 7:09
  • Why have you skipped .executeUpdate()? You should perform it and then commit transaction. Commented Feb 28, 2014 at 7:19
  • @AlexKartishev sorry it's my mistake. i update it and no change Commented Feb 28, 2014 at 7:22

1 Answer 1

6
Transaction tx = null;
    tx = session.beginTransaction();
    boolean b = false;
    if (b == false) {
        Query query = session
                .createQuery(" update Issue set dueStatus = 'true' where returnDate='"
                        + a + "'");
    query.executeUpdate();
    tx.commit();

You have forgotten to execute update before committing transaction.

Sign up to request clarification or add additional context in comments.

2 Comments

May be some cache problems, try to close Session after committing transaction.
For sure it is not an issue, but to prevent some problems, delete second executeUpdate() from System.out.println(). As it is performed twice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.