mirror of
https://gitee.com/jmix/cuba.git
synced 2024-12-04 20:28:00 +08:00
PersistenceHelper.isNew returns false for new instances during transaction. #PL-4420
This commit is contained in:
parent
1c571fd30b
commit
0336e1aea5
@ -64,7 +64,7 @@ public class PersistenceTest extends CubaTestCase {
|
||||
server.setName("localhost");
|
||||
server.setRunning(true);
|
||||
em.persist(server);
|
||||
assertFalse(PersistenceHelper.isNew(server));
|
||||
assertTrue(PersistenceHelper.isNew(server));
|
||||
assertTrue(PersistenceHelper.isManaged(server));
|
||||
assertFalse(PersistenceHelper.isDetached(server));
|
||||
|
||||
|
@ -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.
|
||||
* @param entity entity instance
|
||||
* @return <li>true if the instance is new or if it is not a persistent entity
|
||||
* <li>false if it is Managed or Detached
|
||||
* @return <li>true if the instance is new or if it is not a persistent entity, or if it is actually in Managed state
|
||||
* but newly-persisted in this transaction
|
||||
* <li>false if it is not new Managed or Detached
|
||||
*/
|
||||
public static boolean isNew(Object entity) {
|
||||
if (entity instanceof BaseEntity && ((BaseEntity) entity).isDetached()) {
|
||||
return false;
|
||||
}
|
||||
if (entity instanceof PersistenceCapable) {
|
||||
return ((PersistenceCapable) entity).pcGetStateManager() == null;
|
||||
return ((PersistenceCapable) entity).pcGetStateManager() == null
|
||||
|| ((PersistenceCapable) entity).pcGetStateManager().isNew();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -43,7 +45,7 @@ public class PersistenceHelper {
|
||||
* Determines whether the instance is <em>Managed</em>, i.e. attached to a persistence context.
|
||||
* @param entity entity instance
|
||||
* @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) {
|
||||
if (entity instanceof PersistenceCapable) {
|
||||
|
Loading…
Reference in New Issue
Block a user