我有一个非常奇怪的问题。我正在使用 Hibernate 4.1.7 和 Postgres 9.1。在我的实体类中,我使用注释定义了一个过滤器:
@FilterDef(name = "filterName", parameters=@ParamDef(name="paramInt", type="integer" ))
然后我将@Filter 添加到集合中。如果过滤器是这样定义的:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "linkField")
@Filter(name = "filterName", condition = "fieldVal=0")
private Collection<ChildClass> children;
一切正常,集合被正确过滤,我只看到 fieldVal 为 0 的孩子。另一方面,如果我这样做:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "linkField")
@Filter(name = "filterName", condition = "fieldVal=:paramInt")
private Collection<ChildClass> children;
我在我的代码中设置了参数,“过滤器”设置正确(在调试器中检查它)但是,在访问集合时,它会引发以下异常:
java.lang.NullPointerException:
at org.hibernate.engine.spi.QueryParameters.processFilters(QueryParameters.java:492)
at org.hibernate.engine.spi.QueryParameters.processFilters(QueryParameters.java:462)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1688)
at org.hibernate.loader.Loader.doQuery(Loader.java:832)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:293)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:263)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2094)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:61)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:678)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:82)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1801)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:524)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:212)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:520)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:125)
at org.hibernate.collection.internal.AbstractPersistentCollection$1.doWork(AbstractPersistentCollection.java:152)
at org.hibernate.collection.internal.AbstractPersistentCollection$1.doWork(AbstractPersistentCollection.java:139)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:212)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:138)
at org.hibernate.collection.internal.PersistentBag.size(PersistentBag.java:242)
我没有想法,几天来我一直在试图弄清楚它可能是什么,到处搜索都无济于事。有人知道可能是什么吗?
谢谢!
编辑:在引发异常时更清楚