user3663882
user3663882

Reputation: 7367

TransferObject in the DAO pattern

In the official documentation about the DAO pattern said that there were four main participants in the pattern.

BusinessObject
DataAccessObject
DataSource
TransferObject

Also there says:

The BusinessObject represents the data client. It is the object that requires access to the data source to obtain and store data. A BusinessObject may be implemented as a session bean, entity bean, or some other Java object, in addition to a servlet or helper bean that accesses the data source.

My question is why we can't use the Entity bean as a Transfer Object? I thought it perfectly suits to the definition.

So essentially entity beans can be used both as BusinessObject and TransferObject, can they?

For instance, an entity:

@Entity
@Table(name="tbl")
public class User{
    //Fields, Getters, Setters
}

What's wrong?

Upvotes: 1

Views: 74

Answers (2)

shazin
shazin

Reputation: 21893

Yes, In modern technology stacks this is done. You can use the same POJO for both Business Object and Transfer Object. But there are limitations of this approach.

  1. Can't exclude or include certain fields (You may want to store password but hide it when you transfer to front end)
  2. For different scenarios might require different fields (For create there can be one Transfer Object and for update there can be another).

Because of these reasons it is recommended to have two sets of POJOs.

Upvotes: 1

bedrin
bedrin

Reputation: 4584

Entity is bounded to JPA session and will not work correctly when the session is closed (for example when it is sent to another server) - for example accessing lazy collections will throw an exception

Upvotes: 2

Related Questions