Java Hibernate query.executeUpdate() 不能正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22088231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:32:04  来源:igfitidea点击:

Hibernate query.executeUpdate() not working properly

javahibernatehsqldb

提问by Prasanth A R

Hibernate query.executeUpdate() is not working..

Hibernate query.executeUpdate() 不起作用..

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 the printing 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 the printing value result show correct values. But the database is shows no change..

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

if you know about this please sahre 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();

采纳答案by Alex Kartishev

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.

您忘记在提交事务之前执行更新。