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

NullPointer in JPABase.cascadeOrphans() caused by unused lines #1430

Closed
ketain opened this issue Nov 9, 2022 · 1 comment · Fixed by #1431
Closed

NullPointer in JPABase.cascadeOrphans() caused by unused lines #1430

ketain opened this issue Nov 9, 2022 · 1 comment · Fixed by #1431
Assignees
Milestone

Comments

@ketain
Copy link

ketain commented Nov 9, 2022

Play Version 1.6.x

Operating System (Ubuntu 15.10 / MacOS 10.10 / Windows 10)

Windows 10

JDK (Oracle 1.8.0_72, OpenJDK 1.8.x, Azul Zing)

java version "1.8.0_333"

Expected Behavior

Please describe the expected behavior of the issue, starting from the first action.

  1. attempt to delete entity
  2. JPABase._delete() is called
  3. _delete() calls saveAndCascade()
  4. saveAndCascade() calls cascadeOrphans()
  5. entity is deleted

Actual Behavior

  1. attempt to delete entity
  2. JPABase._delete() is called
  3. _delete() calls saveAndCascade()
  4. saveAndCascade() calls cascadeOrphans() -> throws NullPointer
    5 entity is not deleted; all changes are rolled back as they should

Findings

This is the code of the method which throws the error.

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

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

        if (ce != null) {
            CollectionPersister cp = ce.getLoadedPersister();
            if (cp != null) {
                Type ct = cp.getElementType();
                if (ct instanceof EntityType) {
                    EntityEntry entry = pc.getEntry(base);
                    String entityName = entry.getEntityName(); // <- Exception thrown here
                    entityName = ((EntityType) ct).getAssociatedEntityName(session.getFactory());
                    if (ce.getSnapshot() != null) {
                        Collection orphans = ce.getOrphans(entityName, persistentCollection);
                        for (Object o : orphans) {
                            saveAndCascadeIfJPABase(o, willBeSaved);
                        }
                    }
                }
            }
        }
    }

The reason for these 3 lines escapes me:

EntityEntry entry = pc.getEntry(base);
String entityName = entry.getEntityName(); // <- Exception thrown here
entityName = ((EntityType) ct).getAssociatedEntityName(session.getFactory());

entityName is initialized with a value that is immediatly overwritten in the next line. Could this not be rewritten as:

 String  entityName = ((EntityType) ct).getAssociatedEntityName(session.getFactory());

Has someone else encountered this before?

@asolntsev
Copy link
Contributor

@ketain I haven't encountered this problem, but I clearly see this line should be removed.
Feel free to submit a pull request!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants