Using JPA entities in JSF. Which is the best strategy to prevent LazyInitializationException?

后端 未结 6 1917
执念已碎
执念已碎 2020-11-28 11:48

Would like to hear experts on best practice of editing JPA entities from JSF UI.

So, a couple of words about the problem.

Imagine I have the persisted object

6条回答
  •  青春惊慌失措
    2020-11-28 11:57

    A very common approach is to create an open entity manager in view filter. Spring provides one (check here).

    I can't see that you're using Spring, but that's not really a problem, you can adapt the code in that class for your needs. You can also check the filter Open Session in View, which does the same, but it keeps a hibernate session open rather than an Entity Manager.

    This approach might not be good for your application, there're a few discussions in SO about this pattern or antipattern. Link1. I think that for most applications (smalish, less than 20 concurrent users) this solution works just fine.

    Edit

    There's a Spring class ties better with FSF here

提交回复
热议问题