Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/1430 do not get unused value #1431

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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