Skip to content

Commit

Permalink
Merge pull request #1431 from aleksandy/bugfix/1430-do-not-get-unused…
Browse files Browse the repository at this point in the history
…-value

Bugfix/1430 do not get unused value
  • Loading branch information
xael-fry committed Jul 15, 2023
2 parents f8812ce + 618d846 commit 586609b
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 deletions framework/src/play/db/jpa/JPABase.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void _save() {
em(dbName).persist(this);
PlayPlugin.postEvent("JPASupport.objectPersisted", this);
}
avoidCascadeSaveLoops.set(new HashSet<JPABase>());
avoidCascadeSaveLoops.set(new HashSet<>());
try {
saveAndCascade(true);
} finally {
Expand All @@ -63,7 +63,7 @@ public void _save() {
throw e;
}
}
avoidCascadeSaveLoops.set(new HashSet<JPABase>());
avoidCascadeSaveLoops.set(new HashSet<>());
try {
saveAndCascade(false);
} finally {
Expand All @@ -76,7 +76,7 @@ public void _delete() {
String dbName = JPA.getDBName(this.getClass());

try {
avoidCascadeSaveLoops.set(new HashSet<JPABase>());
avoidCascadeSaveLoops.set(new HashSet<>());
try {
saveAndCascade(true);
} finally {
Expand All @@ -92,7 +92,7 @@ public void _delete() {
throw e;
}
}
avoidCascadeSaveLoops.set(new HashSet<JPABase>());
avoidCascadeSaveLoops.set(new HashSet<>());
try {
saveAndCascade(false);
} finally {
Expand All @@ -113,7 +113,7 @@ public Object _key() {

// ~~~ SAVING
public transient boolean willBeSaved = false;
static final transient ThreadLocal<Set<JPABase>> avoidCascadeSaveLoops = new ThreadLocal<>();
static final ThreadLocal<Set<JPABase>> avoidCascadeSaveLoops = new ThreadLocal<>();

private void saveAndCascade(boolean willBeSaved) {
this.willBeSaved = willBeSaved;
Expand Down Expand Up @@ -157,27 +157,17 @@ private void saveAndCascade(boolean willBeSaved) {
if (value instanceof PersistentMap) {
if (((PersistentMap) value).wasInitialized()) {

cascadeOrphans(this, (PersistentCollection) value, willBeSaved);
cascadeOrphans((PersistentCollection) value, willBeSaved);

for (Object o : ((Map) value).values()) {
saveAndCascadeIfJPABase(o, willBeSaved);
}
}
} else if (value instanceof PersistentCollection) {
PersistentCollection col = (PersistentCollection) value;
if (((PersistentCollection) value).wasInitialized()) {
cascadeOrphans((PersistentCollection) value, willBeSaved);

cascadeOrphans(this, (PersistentCollection) value, willBeSaved);

for (Object o : (Collection) value) {
saveAndCascadeIfJPABase(o, willBeSaved);
}
} else {
cascadeOrphans(this, col, willBeSaved);

for (Object o : (Collection) value) {
saveAndCascadeIfJPABase(o, willBeSaved);
}
for (Object o : (Collection) value) {
saveAndCascadeIfJPABase(o, willBeSaved);
}
} else if (value instanceof Collection) {
for (Object o : (Collection) value) {
Expand All @@ -199,10 +189,10 @@ private void saveAndCascade(boolean willBeSaved) {
}
}

private void cascadeOrphans(JPABase base, PersistentCollection persistentCollection, boolean willBeSaved) {
private void cascadeOrphans(PersistentCollection persistentCollection, boolean willBeSaved) {
String dbName = JPA.getDBName(this.getClass());

SessionImpl session = ((SessionImpl) JPA.em(dbName).getDelegate());
SessionImpl session = JPA.em(dbName).unwrap(SessionImpl.class);
PersistenceContext pc = session.getPersistenceContext();
CollectionEntry ce = pc.getCollectionEntry(persistentCollection);

Expand All @@ -211,9 +201,7 @@ private void cascadeOrphans(JPABase base, PersistentCollection persistentCollect
if (cp != null) {
Type ct = cp.getElementType();
if (ct instanceof EntityType) {
EntityEntry entry = pc.getEntry(base);
String entityName = entry.getEntityName();
entityName = ((EntityType) ct).getAssociatedEntityName(session.getFactory());
String entityName = ((EntityType) ct).getAssociatedEntityName(session.getFactory());
if (ce.getSnapshot() != null) {
Collection orphans = ce.getOrphans(entityName, persistentCollection);
for (Object o : orphans) {
Expand Down Expand Up @@ -315,16 +303,18 @@ public int hashCode() {
@Override
public String toString() {
Object key = this._key();
String keyStr = "";
StringBuilder keyStr = new StringBuilder(64)
.append(getClass().getSimpleName())
.append('[');
if (key != null && key.getClass().isArray()) {
for (Object object : (Object[]) key) {
keyStr += object.toString() + ", ";
keyStr.append(object.toString()).append(", ");
}
keyStr = keyStr.substring(0, keyStr.length() - 2);
keyStr.setLength(keyStr.length() - 2);
} else if (key != null) {
keyStr = key.toString();
keyStr.append(key);
}
return getClass().getSimpleName() + "[" + keyStr + "]";
return keyStr.append(']').toString();
}

public static class JPAQueryException extends RuntimeException {
Expand Down

0 comments on commit 586609b

Please sign in to comment.