Hibernate query.executeUpdate()无法正常工作

时间:2014-02-28 06:49:44

标签: java hibernate hsqldb

Hibernate query.executeUpdate()无效..

这是更新的代码

   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;
    }

此处打印结果显示正确的值,

但数据库没有变化。

和hibernate代码

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

在控制台中,打印值结果显示正确的值。但数据库显示没有变化..

如果您对此有所了解,请在此处提供答案..

更新

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();

1 个答案:

答案 0 :(得分:5)

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();

在提交交易之前,您忘记执行更新。