This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JPA Entity class facade design through EJB  RSS feed

 
Rob Micah
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen comments in several places that returning JPA entity classes out of an EJB is a bad idea and I can certainly understand why. I'm curious what design patterns people here are using to avoid this. I've been using an interface that defines getter methods on the fields of the entity class and using the return type of my EJB methods to this interface. Is there a better way to do this?
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Micah wrote:I've seen comments in several places that returning JPA entity classes out of an EJB is a bad idea and I can certainly understand why.

Can you show us some sources? Because I don't agree completely. I don't see a problem in returning JPA entities from specific EJBs, as long as you keep their transactional behaviour in mind. As long as the entity is part of the transaction it was retrieved / created with, any changes to the entity will be saved. As soon as the transaction ends, the entity is automatically detached. The latter means that you cannot access any properties that have not yet been (fully) loaded, like linked entities.

I'm curious what design patterns people here are using to avoid this. I've been using an interface that defines getter methods on the fields of the entity class and using the return type of my EJB methods to this interface. Is there a better way to do this?

The interface still has some of the same issues, like unloaded properties not being accessible. What we usually do where I work is create a matching DTO class and convert the entity to an instance of the DTO class. That can be cumbersome though, especially if your entities have many properties.
 
Rob Micah
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry I can't remember the place where I saw this advice but I do remember seeing it. I'll search around a little more. Can you explain what you mean by DTO class? It sounds like you're doing something similar to what I'm doing.
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DTO = data transfer object. It's basically a simple POJO that is actually mostly the same as the entity, but without all the JPA stuff. Also, it will never have bi-directional relationships because those just don't serialize to JSON or XML. So if entity x references entity y and y references x as well, then in the DTO one of these references is simply dropped (most often a ManyToOne side).
 
Rob Micah
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds like what I'm trying to accomplish by creating an entity class that implements an interface and then using that interface as the return type from my EJBs.
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except you still return the entity, with all of its loading and saving issues. With a DTO, you create a new object that is a copy of the entity, and not the entity itself.
 
Rob Micah
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly. Which goes back to my original question of "What strategy do you use to deal with this"? Sounds like you are creating a completely new instance. In the past this is what I have also done but I was just wondering if there was a better way or a design pattern that's common.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!