English 中文(简体)
JPA / @PostPersist @PostUpdate - trade
原标题:JPA / @PostPersist @PostUpdate - transaction

我目前正在使用<条码>@PostPersist和@PostUpdate,并在这些触发器上工作。 我在继续增加一些实体。 问题是,同一交易中究竟是哪些触发因素,如果不可能强迫?

For me it works this way. While I was looking through the logs the transaction isn t existing ( it s commited just before the trigger is launched ) which prevents me ( without REQUIRES_NEW on the persisting method from injected bean ) from saving the additional entities in database. REQUIRED attribute is totally ignored, and MANDATORY attribute do not throw an exception.

这个问题与Junnit有关(因为我是在发展中)。 ?

如果不可能将交易延伸到这一触发点,那么如何确保如果退约发生在<条码>@PostPers和<条码>@PostUpdate之前,这些行动也将得到滚动支持。

问题回答

如果你重新使用春天,你可以总是与您目前的交易主管登记的转账代码,以备您目前进行的交易承诺等活动:

@PostPersist
void onPersist() {
    TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

      @Override
      public void beforeCommit(boolean readOnly) {
        // do work
      }
    });
  }    
}

如果您需要检查交易是否发生或停止,则使用<条码>在Completion(int status)。

详情请上。 TransactionSynchronization s JavaDoc

The firing of a PostPersist event does not indicate that the entity has done a successful commit. The transaction may be rolled back after the firing of the event but before the successful commit. If you in the PostPersist get the entity manager used in the transaction and then do somehting like this:

@PostPersist
void someMethod() {
  EntityManager em = null;
  em = getEntityManagerUsedInTransaction();
  EntityTransaction et = em.getTransaction(); // should return the current transaction
  if (et.isActive() ) {
    // do more db stuff
  }
}

NB: I haven t tried this so it s only speculation (tho I have used the lifetime event trigger extensively for other stuff). I have to add that I don t think this is a good idea. Use the PostPersist to flag that other entities should be persisted and do it in another transaction.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签