使用hibernate不会更新数据库表

时间:2016-02-15 13:59:34

标签: java database hibernate

我想更新数据库中的列,但它不起作用。我无法理解问题是什么,我在这个论坛上尝试了很多建议。 即使在日志中调用了更新,但在数据库中我看不到任何更改。

这是更新WAGON_LIST表中的一些记录的代码:

    @PersistenceContext(unitName = "vimsaddXaPU")
    private Session s;

    Transaction tx;
    List<WagonList> listAlleGleiseZuAktualisieren = new ArrayList<WagonList>();
    for (String gleis : setKwgZukoGleise) {
        listAlleGleiseZuAktualisieren = (List<WagonList>) s.createCriteria(WagonList.class).add(Restrictions.like("actTrack", gleis + "%")).list();
    }

    String actTrack = null;
    Long actPosition = null;

    // update existent gleises with Gleis
    for (WagonList wagon : listAlleGleiseZuAktualisieren) {

        tx = s.beginTransaction();

        if (wagon != null) {
            actTrack = wagon.getActTrack();
            actPosition = wagon.getActPosition();
            String gleis = null;
            if (actPosition < 10) { 
                gleis = actTrack + "0" + actPosition;
            } else {
                gleis = actTrack + actPosition;
            }

            wagon.setActTrack(gleis);

            ***//Here I tried saveOrUpdate, merge and also without calling, beucase Hibernate sees that in wagon.setActTrack(gleis) above a value is changed and should make the update***
            s.saveOrUpdate(wagon);

            tx.commit();
        }
    }

执行循环后,我也尝试使用s.flush(),但它没有帮助。

这是我的persistence.xml

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="vimsaddXaPU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:/VIMSXaDS</jta-data-source>
    <class>hs2n.vims.db.entities.WagonList</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.ejb.metamodel.generation" value="disabled" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
        <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
        <property name="hibernate.cache" value="org.hibernate.cache.EhCacheProvider" />
        <property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory"/>
        <property name="hibernate.archive.autodetection" value="false" />
        <property name="hibernate.max_fetch_depth" value="5" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.default_batch_fetch_size" value="32" />
        <property name="hibernate.cache.use_query_cache" value="true" />
        <property name="hibernate.cache.use_second_level_cache" value="true" />
        <!--   <property name="hibernate.session_factory_name" value="vimshibernate/SessionFactory"/> -->
        <property name="hibernate.cache.provider_configuration_file_resource_path"  value="ehcache.xml" /> 
    </properties>
</persistence-unit>

VIMSXaDS是一个xa-datasource,包含以下内容:

     <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
     <xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>

我正在事务中进行更新,我可以在日志中看到执行更新查询

[STDOUT] (pool-70-thread-1)     update
[STDOUT] (pool-70-thread-1)         TFMS.WAGON_LIST 
[STDOUT] (pool-70-thread-1)     set
[STDOUT] (pool-70-thread-1)         ACT_TRACK=? 
[STDOUT] (pool-70-thread-1)     where
[STDOUT] (pool-70-thread-1)         WAGON_ID=?

preparing statement
Dehydrating entity: [hs2n.vims.db.entities.WagonList#338079199653]
binding parameter [1] as [VARCHAR] - KLS30617
binding parameter [2] as [BIGINT] - 338079199653
Pre-invalidating space [TFMS.WAGON_LIST]
Executing batch size: 1
success of batch update unknown: 0
about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
closing statement
skipping aggressive-release due to flush cycle
registering flush end
aggressively releasing JDBC connection
releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open      ResultSets: 0, globally: 0)]
post flush
begin
after transaction begin
commit
automatically flushing session
flushing session
processing flush-time cascades
done processing cascade ACTION_PERSIST_ON_FLUSH for:    hs2n.vims.db.entities.WagonList
processing cascade ACTION_PERSIST_ON_FLUSH for:  hs2n.vims.db.entities.WagonList
done processing cascade ACTION_PERSIST_ON_FLUSH for: hs2n.vims.db.entities.WagonList
processing cascade ACTION_PERSIST_ON_FLUSH for: hs2n.vims.db.entities.WagonList
done processing cascade ACTION_PERSIST_ON_FLUSH for: hs2n.vims.db.entities.WagonList
processing cascade ACTION_PERSIST_ON_FLUSH for: hs2n.vims.db.entities.WagonList
done processing cascade ACTION_PERSIST_ON_FLUSH for: hs2n.vims.db.entities.WagonList
dirty checking collections
Flushing entities and processing referenced collections
hs2n.vims.db.entities.WagonList.actTrack is dirty
Found dirty properties [[hs2n.vims.db.entities.WagonList#338079205096]] : [actTrack]
Updating entity: [hs2n.vims.db.entities.WagonList#338079205096]
Processing unreferenced collections
Scheduling collection removes/(re)creates/updates
Flushed: 0 insertions, 1 updates, 0 deletions to 20 objects
Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections

编辑:似乎在代码中进一步调用sql过程时更新数据库表,而不是在刷新或提交事务时更新。 使用或不使用Transaction更新表(会话也可以处理事务),但问题是它在刷新或提交调用时没有更新,而是在过程调用时更新。

CallableStatement call = connection.prepareCall("{ call TFMS.STANDORT(?,?,?,?,?,?,?,?) }");

    call.setLong(1, p_kwnr);
    call.setString(2, p_gleis);
    call.setLong(3, p_position);
    call.setDate(4, p_eingangsdatum);
    call.setLong(5, p_flag);        
    call.setString(6, p_user);
    call.setLong(7, p_plantID);
    call.registerOutParameter(8, Types.INTEGER);
    call.execute();

0 个答案:

没有答案