3

Since the "open session in view" pattern have some drawbacks (see Why is Hibernate Open Session in View considered a bad practice?), I am wondering what is considered the best approach in displaying results from a hibernate query to a jsp page?

One method I thought of is put a java.util.list object in the request and output the contents to the jsp page. Are there other/better methods?

Community
  • 1
  • 1
lightning_missile
  • 2,821
  • 5
  • 30
  • 58
  • The way I understand why OSIV is a bad practice is that you pass your DB objects into your front end software - a simple generic example would be to have DTO out of your entity objects, created by your backend and passed to your frontend. – Smutje Aug 02 '15 at 10:26

1 Answers1

1

The best way is to use DTO projections for your UI views. This way you can avoid LazyInitializationExceptions and make sure you only fetch what you need in a certain view. From a performance point of view, nothing beats an SQL projection anyway.

The DTO projection looks like this:

select new my.package.UserInfo(u.name, u.age, u.gender)
from Users u
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911