3

I am using this code to get my entity from the database via the primary key:

@Override
public Entity read(final long key) throws DAOException {
    try {
        EntityManager em = PM.getEntityManager();
        em.getTransaction().begin();
        Sportart result = em.find(Entity .class, key);
        em.getTransaction().commit();
        em.close();
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        throw new DAOException("Error reading Entity.");
    }
}

This is the stackTrace:

java.lang.NullPointerException
    at org.hibernate.engine.internal.StatefulPersistenceContext.getLoadedCollectionOwnerOrNull(StatefulPersistenceContext.java:756)
    at org.hibernate.event.spi.AbstractCollectionEvent.getLoadedOwnerOrNull(AbstractCollectionEvent.java:75)
    at org.hibernate.event.spi.InitializeCollectionEvent.<init>(InitializeCollectionEvent.java:36)
    at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1931)
    at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:558)
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:260)
    at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554)
    at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:142)
    at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:447)
    at abc.de.App.entities.Entity.hashCode(Entity.java:75)
    at java.util.HashMap.hash(Unknown Source)
    at java.util.HashMap.put(Unknown Source)
    at java.util.HashSet.add(Unknown Source)
    at java.util.AbstractCollection.addAll(Unknown Source)
    at org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:344)
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:251)
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:238)
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:211)
    at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:168)
    at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishLoadingCollections(AbstractRowReader.java:255)
    at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:218)
    at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:140)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:138)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
    at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:100)
    at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:693)
    at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:92)
    at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1933)
    at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:558)
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:260)
    at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554)
    at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:142)
    at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:447)
    at abc.de.App.entities.Other.hashCode(Other.java:206)
    at java.util.HashMap.hash(Unknown Source)
    at java.util.HashMap.put(Unknown Source)
    at java.util.HashSet.add(Unknown Source)
    at java.util.AbstractCollection.addAll(Unknown Source)
    at org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:344)
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:251)
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:238)
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:211)
    at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:168)
    at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishLoadingCollections(AbstractRowReader.java:255)
    at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:218)
    at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:140)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:138)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
    at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:186)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4126)
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:503)
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:468)
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:213)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:275)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:151)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1106)
    at org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:176)
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2587)
    at org.hibernate.internal.SessionImpl.get(SessionImpl.java:991)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1110)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1068)
    at abc.de.App.dao.implementations.DAOImpl.read(DAOImpl.java:44)
    at abc.de.App.dao.implementations.DAOImpl.read(DAOImpl.java:1)
    at abc.de.App.controllers.Controller.saveData(Controller.java:73)
    at abc.de.App.views.View.lambda$2(View.java:183)
    at java.lang.Thread.run(Unknown Source)

And the Entity:

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity
public class Entity implements Model {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long Entity_id;
    @Column(nullable = false)
    private String bez;
    @Column(nullable = false)
    private String bes;
    @ManyToMany(mappedBy = "Entities", fetch = FetchType.EAGER)
    private Set<Other> other = new HashSet<>();


    public Entity() {

    }

    public long getEntity_id() {
        return Entity_id;
    }

    public void setEntity_id(final long Entityid) {
        this.Entity_id = Entityid;
    }

    public String getbez() {
        return bez;
    }

    public void setbez(final String bez) {
        this.bez = bez;
    }

    public String getbes() {
        return bes;
    }

    public void setbes(final String bes) {
        this.bes = bes;
    }

    public Set<Other> getOther() {
        return other;
    }

    public void setOther(final Set<Other> Other) {
        this.other = other;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((bes == null) ? 0 : bes.hashCode());
        result = prime * result
                + ((bez == null) ? 0 : bez.hashCode());
        result = prime * result
                + ((other == null) ? 0 : other.hashCode());
        result = prime * result + (int) (Entity_id ^ (Entity_id >>> 32));
        return result;
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Entity other = (Entity) obj;
        if (bes == null) {
            if (other.bes != null) {
                return false;
            }
        } else if (!bes.equals(other.bes)) {
            return false;
        }
        if (bez == null) {
            if (other.bez != null) {
                return false;
            }
        } else if (!bez.equals(other.bez)) {
            return false;
        }
        if (other == null) {
            if (other.other != null) {
                return false;
            }
        } else if (!other.equals(other.other)) {
            return false;
        }
        if (Entity_id != other.Entity_id) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Entity [Entity_id=" + Entity_id + ", bez="
                + bez + ", bes=" + bes
                + ", other=" + other + "]";
    }

}

I am using the same code for my other entity and there it works fine.

What could be the cause here?

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
user3813234
  • 1,580
  • 1
  • 29
  • 44
  • 1
    `java.lang.NullPointerException [...] at abc.de.App.entities.Entity.hashCode(Entity.java:75)` - which line is line 75? In the code you pasted, line 75 is empty. – sina Nov 24 '15 at 12:02

1 Answers1

4

This error comes with filling up a console with repeating error message. It looks like an infinite recursion. So where it comes from? Lets look at the error message and we will see that somewhere under the hood hibernate calculates hash for your entity. In your Entity's hashCode() you call other.hashCode() which will sum hashes of elements in set calling Other.hashCode(). Most likely your Other.hashCode() calls Entities.hashCode() which will sum hashes calling Entity.hashCode() and so on ... - there you get infinite recursion.

dimathe47
  • 346
  • 1
  • 4
  • 8
  • 1
    Thanks, this saved me from a lot of troubleshooting. I am using Lombok, and it was creating a hashCode function for me without me even knowing it. – Mike Nakis Dec 09 '16 at 14:27
  • It's not a recursion issue, it's a bug in some versions of Hibernate. Easiest in Lombok is to just exclude the nested set field from your hashcodes via annotation. Please see my answer in https://stackoverflow.com/questions/46140456/spring-data-findone-nullpointerexception/63715074#63715074 which references some other posts with more detail. – orbfish Sep 03 '20 at 00:16