PersistenceHelper.isNew returns false for new instances during transaction. #PL-4420

This commit is contained in:
Konstantin Krivopustov 2014-10-14 13:12:27 +00:00
parent 1c571fd30b
commit 0336e1aea5
2 changed files with 7 additions and 5 deletions

View File

@ -64,7 +64,7 @@ public class PersistenceTest extends CubaTestCase {
server.setName("localhost"); server.setName("localhost");
server.setRunning(true); server.setRunning(true);
em.persist(server); em.persist(server);
assertFalse(PersistenceHelper.isNew(server)); assertTrue(PersistenceHelper.isNew(server));
assertTrue(PersistenceHelper.isManaged(server)); assertTrue(PersistenceHelper.isManaged(server));
assertFalse(PersistenceHelper.isDetached(server)); assertFalse(PersistenceHelper.isDetached(server));

View File

@ -26,15 +26,17 @@ public class PersistenceHelper {
/** /**
* Determines whether the instance is <em>New</em>, i.e. just created and not stored in database yet. * Determines whether the instance is <em>New</em>, i.e. just created and not stored in database yet.
* @param entity entity instance * @param entity entity instance
* @return <li>true if the instance is new or if it is not a persistent entity * @return <li>true if the instance is new or if it is not a persistent entity, or if it is actually in Managed state
* <li>false if it is Managed or Detached * but newly-persisted in this transaction
* <li>false if it is not new Managed or Detached
*/ */
public static boolean isNew(Object entity) { public static boolean isNew(Object entity) {
if (entity instanceof BaseEntity && ((BaseEntity) entity).isDetached()) { if (entity instanceof BaseEntity && ((BaseEntity) entity).isDetached()) {
return false; return false;
} }
if (entity instanceof PersistenceCapable) { if (entity instanceof PersistenceCapable) {
return ((PersistenceCapable) entity).pcGetStateManager() == null; return ((PersistenceCapable) entity).pcGetStateManager() == null
|| ((PersistenceCapable) entity).pcGetStateManager().isNew();
} }
return true; return true;
} }
@ -43,7 +45,7 @@ public class PersistenceHelper {
* Determines whether the instance is <em>Managed</em>, i.e. attached to a persistence context. * Determines whether the instance is <em>Managed</em>, i.e. attached to a persistence context.
* @param entity entity instance * @param entity entity instance
* @return <li>true if the instance is managed, * @return <li>true if the instance is managed,
* <li>false if it is New or Detached, or if it is not a persistent entity * <li>false if it is New (and not yet persisted) or Detached, or if it is not a persistent entity
*/ */
public static boolean isManaged(Object entity) { public static boolean isManaged(Object entity) {
if (entity instanceof PersistenceCapable) { if (entity instanceof PersistenceCapable) {