0

I've just discovered quite an old piece of code which is directly using a JPA entity in a JSF bean. To me this seems wrong as I've always looked to keep the entity in the back-end/service layer and then created separate objects to use in the front-end/bean.

Nothing is being set on the entity and there's no transaction open, so there's no risk of changing anything, but I'm still concerned that using the getters will query the DB every time as it's all lazily loaded. So I feel it would be better to get the data needed up front and load it into another object.

Having discussed this with some others on my team a few people don't seem concerned about it, but it doesn't seem right to me. I'm hoping someone could give me some information on what is the correct use and why?

Thanks

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
hello123
  • 951
  • 2
  • 15
  • 25

1 Answers1

1

Technically is possible to pass a JPA entity to a JSF bean, but is not a good design.

If you try to follow the S.O.L.I.D. principles, you will break the Single Responsibility Principle, because you are mixing different layer of abstraction in the same class.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • Yes I could see that it's possible and would work, but it just seems like bad practice to me. I was just struggling to make a good argument as to why it wasn't good. Thanks a lot for that, exactly what I was looking for! – hello123 Jun 15 '16 at 07:47