java - Hibernate 4.2.20 对象 HashCode 方法导致 NullPointerException

标签 java hibernate hibernate-mapping

我使用的是 Hibernate 4.2.3.FINAL,没有任何问题。当我尝试升级到最新版本 4.2.20.FINAL 时,我在尝试执行加载项目列表的查询时收到 NullPointerException。

从底部的堆栈跟踪中,您可以看到我的类在 hashCode 方法上失败了:

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

这个 hashCode 方法看起来像这样:

@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();
}

这一行失败了:

hcb.append(bundledTicketProductions);

那个对象是另一个 hibernate 对象:

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

似乎在加载此 BundledPackageTicket 对象时,Hibernate 在加载对此子对象的引用时遇到问题。从历史上看,这很有效。

为了完整起见,HQL 如下所示:

@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);
}

堆栈跟踪的相关部分:

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]

请帮忙!

最佳答案

我们在 Hibernate 4.1.8 中也遇到了这个问题,但是有嵌套集。我们将最深的 Set 更改为 List;这解决了我们的问题。

它首先发生在将集合包含在 hashCode 之后,在此之前它工作正常。

关于java - Hibernate 4.2.20 对象 HashCode 方法导致 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33024525/

相关文章:

c# - 为什么 .NET 中只有一个 NoSQL 平台,而 Java 中有数十个?

java - @ActiveProfiles 不适用于 AbstractTestNGSpringContextTests

hibernate - Coldfusion HQL 查询异常 - 意外标记

java - Java 中的 128 位比较交换

java - 从数据库创建 Hibernate 映射文件和 PJOS 时出错

java - Hibernate 不尊重 MySQL auto_increment 主键字段

hibernate - Spring 和/或 hibernate : Saving many-to-many relations from one side after form submission

hibernate - 为什么 Hibernate 会尝试加载 "not-found=ignore"关联?

java - Hibernate关系一对多映射问题

java - Google App Engine HR 数据存储 - Java - 以编程方式删除实体