30

I cannot find difference between them. Does anyone know how to differentiate them?

0

8 Answers 8

20

POJO or "Plain Old Java Object" is a name used to describe "ordinary" Java objects, as opposed to EJBs (originally) or anything considered "heavy" with dependencies on other technologies.

DTO or "Data Transfer Object" is an object for... well... transferring data, usually between your "business" classes and persistence layer. It typically is a behavior-less class much like a C-style struct. They are an outdated concept.

Sign up to request clarification or add additional context in comments.

5 Comments

Not necessarily an outdated concept - they're a design pattern, and are still used to solve the general problem they address. They're just not used as often now because they were mostly used to address problems with EJBs - which aren't used as often now...
The reason I say it is outdated is because the current trend is to pass fully OO "business" or "domain" objects with behavior, rather than near useless non-OO objects. I suspect this trend is due to refinements in OO philosophy and to technologies for mapping objects to other representations (such as JPA, Hibernate, JAXB, etc.).
With JAXB, JPA etc.. you don't map your objects to those represetantions as such. You just end up annotating your domain objects...
When you annotate something to mean "this class attribute is stored in this database column", you are mapping your objects to another representation. Annotation or mapping file, it is still mapping. That's what I meant. And as you say, they are on "domain" objects, i.e. not DTOs.
It would be easier to understand if someone put a class code! An image tells more than a thousand words!
12

A POJO is just a simple Java object, the acronym is used to emphasize that it really is nothing special.

A DTO is a Data Transfer Object which is used to encapsulate data that is transferred over a connection between layers or subsystems. See the wikipedia article, it's also a Core J2EE pattern (http://www.oracle.com/technetwork/java/transferobject-139757.html).

http://en.wikipedia.org/wiki/Data_transfer_object

2 Comments

I'm not actually sure where the term POJO came from, I don't think it existed around the time of the DTO back in the dark evil smelly days of EJB 1.x
And this explains some of the POJO origins: en.wikipedia.org/wiki/Plain_Old_Java_Object
8

All DTOs are POJOs, but not all POJOs are DTOs. An example of POJO that is not a DTO is a business class that contains state and behavior (business logic).

2 Comments

"All DTOs are POJOs" is very confusing. DTOs are Serializable - POJOs don't implement any interface or extend any class. This means DTOs can't be POJOs as they already implement a pre-specified interface (Serializable).
@EdwardQuixote Yes, DTOs are serializable and POJOs don't need to be. That's why we can say DTOs are POJOs but not all POJOs are DTOs. Another way to read this is to say that DTOs are a specialization of POJOs and as such add features (e.g., serialization).
5

DTO (Data transfer object) : Is a Core J2EE design pattern used for transferring data within the system.DTO Pattern


POJO (Plain Old Java Object) : It is just an acronym people use for suggesting that is a simple java object (which nowadays is heavily annotated for doing some meaning full work).

DTO Pattern
J2EE Pattern Catalog

Comments

5

DTO is pojo, but pojo is not dto, because pojo can have more behavior but DTO just basically no behavior

Oracle document has clear description.

Comments

3

A POJO can have behavior. The book POJOs in Action details use of POJOS for application development. DTOs are data containers that help transfer data from one layer to another. DTOs aren't supposed to contain any behavior.

Comments

1

I could understand the difference between POJO and DTO from this sentence of DTO's wiki:

DTOs are simple objects that should not contain any business logic but may contain serialization and deserialization mechanisms for transferring data over the wire.

Also, DTO is perfectly visualized and described in detail in Martin Fowler's Catalog of Patterns of Enterprise Application Architecture

Comments

-3

POJO = Plain Old Java Object

DTO = Data Transfer Object

-- Edit

Well, this is assuming you don't know what the acronyms mean. A Pojo is just an object that is free from any sort of inheritance chain. A DTO exists in your data model, so probably follows a strict chain relating it to a given entity.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.