Question

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();
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top