Hibernate 4.2.20 Object HashCode Method Causing NullPointerException

筅森魡賤 提交于 2021-02-10 03:31:13

问题


I was using Hibernate 4.2.3.FINAL without any issues. When I attempted to upgrade to the latest version 4.2.20.FINAL, I received a NullPointerException when trying to execute a query that loads a list of items.

From the stacktrace at the bottom, you can see that my class fails on the hashCode method:

com.ovationtix.dao.model.BundledPackageTicket.hashCode(BundledPackageTicket.java:89)

This hashCode method looks like this:

@Override
public int hashCode() {
    HashCodeBuilder hcb = new HashCodeBuilder();
    hcb.append(id);
    hcb.append(displayOrder);
    hcb.append(ticketPackage);
    hcb.append(anySeries);
    hcb.append(deleted);
    hcb.append(bundledTicketProductions);
    hcb.append(createDate);
    return hcb.toHashCode();
}

It fails on this line:

hcb.append(bundledTicketProductions);

That object is another hibernate object:

@OneToMany(mappedBy="bundledPackageTicket", fetch=FetchType.LAZY, cascade={CascadeType.ALL}, orphanRemoval=true)
private Set<BundledTicketProduction> bundledTicketProductions = new HashSet<>();

It seems that while loading this BundledPackageTicket object, Hibernate is having issues loading the reference to this child object. Historically, this worked.

For completeness, the HQL looks like this:

@Override
public List<TicketPackage> getBundledPackagesByIds(final Collection<Integer> packageIds) {
    final String hql =
            "SELECT " +
                    "tp " +
                    "FROM " +
                    "TicketPackage tp " +
                    "INNER JOIN FETCH tp.packagePrices " +
                    "INNER JOIN FETCH tp.bundledPackage bp " +
                    "INNER JOIN FETCH bp.packageTickets bt " +
                    "INNER JOIN FETCH bt.bundledTicketProductions bProd " +
                    "LEFT JOIN FETCH bProd.sections sec " +
                    "LEFT JOIN FETCH bProd.priceLevels pl " +
                    "WHERE " +
                    "tp.id IN (:packageIds) " +
                    "AND tp.packageType = :packageTypeBundled ";

    final Query query = createQuery(hql);
    query.setParameterList("packageIds", packageIds);
    query.setParameter("packageTypeBundled", TicketPackageType.bundled);

    return list(query);
}

The relevant portion of the stack trace:

15:18:49.272 [127.0.0.1]-[http-nio-8082-exec-9] ERROR | c.t.w.action.ActionExceptionHandler.logException[103] | null
java.lang.NullPointerException: null
    at org.hibernate.engine.internal.StatefulPersistenceContext.getLoadedCollectionOwnerOrNull(StatefulPersistenceContext.java:756) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.event.spi.AbstractCollectionEvent.getLoadedOwnerOrNull(AbstractCollectionEvent.java:75) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.event.spi.InitializeCollectionEvent.<init>(InitializeCollectionEvent.java:36) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1931) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:559) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:261) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:555) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:143) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:447) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:909) ~[commons-lang-2.6.jar:2.6]
    at com.ovationtix.dao.model.BundledPackageTicket.hashCode(BundledPackageTicket.java:89) ~[ot-dao-1.11.17-SNAPSHOT.jar:na]
    at java.util.HashMap.hash(HashMap.java:338) ~[na:1.8.0_45]
    at java.util.HashMap.put(HashMap.java:611) ~[na:1.8.0_45]
    at java.util.HashSet.add(HashSet.java:219) ~[na:1.8.0_45]
    at java.util.AbstractCollection.addAll(AbstractCollection.java:344) ~[na:1.8.0_45]
    at org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:344) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:251) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:238) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:211) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:1157) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1126) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.loader.Loader.processResultSet(Loader.java:973) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.loader.Loader.doQuery(Loader.java:921) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2554) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2540) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.loader.Loader.list(Loader.java:2365) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:497) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:236) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1300) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at com.ovationtix.dao.hibernate.impl.BaseDAOHibernate.list(BaseDAOHibernate.java:60) ~[ot-dao-1.11.17-SNAPSHOT.jar:na]
    at com.ovationtix.bundled.dao.impl.BundledPackageDAOImpl.getBundledPackagesByIds(BundledPackageDAOImpl.java:105) ~[ot-checkout-1.1.17-SNAPSHOT.jar:na]

Please help!


回答1:


We also had this problem in Hibernate 4.1.8, but with nested sets. We changed the deepest Set to a List; this solved our problem.

It first occurred after including the set in the hashCode, before that it worked fine.



来源:https://stackoverflow.com/questions/33024525/hibernate-4-2-20-object-hashcode-method-causing-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!