I need to do some processing on an Entity. I'd like it to be outside a transaction. The thing is this entity contains some lazy-loaded fields so that my program fails on accessing some of them because of not having a session active.

The most natural thing to do would be to fully initialize such entity once it's loaded but still in transactional scope (before detaching it). But I can't find how to do that. Is there really no simple method do such a trivial thing? I'd like to stay behind JPA spec.

For some reason fetch all properties in jpql does not work.

有帮助吗?

解决方案

Just call a method on the lazy proxies you want to initialize:

SomeEntity e = ...;
e.getFoos().size(); // now foos is initialized
e.getBar().getName(); // now bar is initialized

To load the whole state at once using JPQL, you need to use fetch joins:

select e from SomeEntity e
left join fetch e.foos
left join fetch e.bar
where ...
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top