Try using version Hibernate Search 4.5.0.Final : it looks like you're hitting HSEARCH-1260, which we recently resolved.
Transient method in Hibernate Search referencing proxy while MassIndexing
-
03-10-2022 - |
Вопрос
I am using a MassIndexer to index my documents. I have a method annotated with @Transient that references a lazily initialized @OneToMany collection like this:
@OneToMany
@JoinColumns({
@JoinColumn(name = "insertForeignKeyHere", referencedColumnName = "insertPrimaryKeyHere"),... })
@NotFound(action = NotFoundAction.IGNORE)
public Set<AdditionalOption> getAdditionalOptions() {
return this.additionalOptions;
}
@Transient
@IndexedEmbedded
public Set<AdditionalOption> getActiveAdditionalOptions() {
Set<AdditionalOption> ret = new HashSet<>();
//the next line produces the error
for (AdditionalOption addOpt : this.getAdditionalOptions()) {
//do stuff.
}
return ret;
}
Whenever I try to Index this document with a MassIndexer and with no @OneToMany(fetch = FetchType.EAGER) I get this Exception:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: <...>, could not initialize proxy - no Session
Any thoughts on how to do this without the EAGER fetching? (I have 4 or 5 collections that would need eager fetching if this didn't work differently -> HUGE performance problems)
Thanks in advance.
btw: I am using
<hibernate.version>4.3.1.Final</hibernate.version>
<hibernate.search.version>4.5.0.Alpha2</hibernate.search.version>
<lucene.version>3.6.2</lucene.version>
Решение
Другие советы
If there is no other way on doing this, I will do it with this work-around (with different bean-classes than in the first posting). But I don't really like it.
public static FeatureValueRepository featureValueRepository;
private static final Lock featureValueRepositoryLock = new ReentrantLock();
private static FeatureValueRepository getFeatureValueRepository() {
featureValueRepositoryLock.lock();
try {
if (featureValueRepository == null) {
//ContextProvider is a custom class in our project
featureValueRepository = ContextProvider.getContext().getBean(
FeatureValueRepository.class);
}
return featureValueRepository;
} finally {
featureValueRepositoryLock.unlock();
}
}
And then call a method that queries by the root-bean's id.