私はlazy="extra"
属性の1対多コレクションを持っています。トランザクション外の余分な遅延コレクションのサイズ()の取得
Class Foo {
List<Bar> barList;
}
上記のクラスコレクションを含むlazy="false"
初期化クラスがあります。
Class FooContainer {
List<Foo> fooList;
}
私はサービスとFooContainerエンティティを取得するので、私はトランザクションの外に.size()
を使用する必要があります。
FooContainer fc = serviceImpl.getFCById(fooContainerId);
このようにfc.getFooList()が初期化されます。それから私はbarList
のサイズだけを取得する必要があります。コレクション全体を取得する必要はありません。
for(Foo currentItem: fc.getFooList()) {
int barSize = currentItem.getBarList().size();
}
これは、LazyInitializationExceptionが表示される場合です。 このメソッドを呼び出そうとしました。
@Transactional
Class AnotherServiceImpl {
int getBarListSize(Foo foo){
return foo.getBarList().size();
}
}
セッションがないために動作しませんでした。 getHibernateTemplate().getCurrentSession.initialize(foo.getBarList())
をAnotherServiceImpl
に入れることはできますが、これはうまくいきますが、おそらくすべての項目を取得することになりますが、これは受け入れられません。
フェッチせずにコレクションサイズを取得する方法はありますか?コメントMethod where I get FooContainer fc and call .size() is the same method and it is not @Transactional
と一緒
関連:https://stackoverflow.com/questions/2198528/hibernate-hql-get-count-of-results-without-actually-returning-them – Moira
私はそれが管理されているエンティティの問題ですか疑います。 FooContainerとgetBarListSize呼び出しメソッドの作成にTransaction-Propagationが使用されるものを追加する必要があります。 – aschoerk
@aschoerk 'ServiceImpl'と' AnotherServiceImpl'にはデフォルトの伝播があります。 'FooContainer fc'を取得し、' .size() 'を呼び出すメソッドは同じメソッドであり、' @ Transactional'ではありません。 – Nalmelune