gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:35:00 25 4
gpt4 key购买 nike

我使用的是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com