0

I am wondering if it is possible to make a JPA implementation throw an exception when a managed BO is modified outside a transaction of the managing entity manager. For example:

BO someBO = new BO();
entityManager.getTransaction().begin();
entityManager.persist(someBO);
entityManager.getTransaction().commit();

someBO.setSomeField(someValue);

Is there a possible configuration of a JPA implementation that results in an exception in the last line?

We are migrating from JDO 1.0, where such an exception would be thrown in the last line when not explicitly configured otherwise.

gchris
  • 1
  • 1
  • 1
    There is no JPA property for that. Each implementation will have its own feature, like for example DataNucleus JPA does – Neil Stockton Mar 08 '16 at 10:55

1 Answers1

1

An entity is attached to a PersisteceContext only within a transaction, when the EntityManager works in PersistenceContextType.TRANSACTION mode, or longer when it uses the PersistenceContextType.EXTENDED. If an entity is not attached to a PersisteceContext, then it does not care for the entity. I can imagine, that a combination of the extended mode and TransactionAttributeType.REQUIRED would behave like this, but you would need a layer that encapsulates someBO.setSomeField(someValue). To modify entity, so that it modifies the content of the database, makes sense only in a context of a transaction.

wojtus
  • 107
  • 8