30

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

codeforester
  • 39,467
  • 16
  • 112
  • 140
d1ck50n
  • 1,331
  • 2
  • 16
  • 20

8 Answers8

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.

SingleShot
  • 18,821
  • 13
  • 71
  • 101
  • 13
    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... – Nate Sep 15 '09 at 13:21
  • 3
    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.). – SingleShot Sep 15 '09 at 14:38
  • With JAXB, JPA etc.. you don't map your objects to those represetantions as such. You just end up annotating your domain objects... – Jonathan Holloway Sep 15 '09 at 15:13
  • 4
    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. – SingleShot Sep 15 '09 at 15:42
  • 4
    It would be easier to understand if someone put a class code! An image tells more than a thousand words! – Mr. DMX Mar 16 '17 at 15:43
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

Slava Babin
  • 708
  • 2
  • 12
  • 30
Jonathan Holloway
  • 62,090
  • 32
  • 125
  • 150
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).

Paulo Merson
  • 13,270
  • 8
  • 79
  • 72
  • 2
    "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). – Edward Quixote Apr 07 '20 at 14:44
  • 1
    @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). – Paulo Merson Apr 08 '20 at 13:03
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

user10398
  • 480
  • 4
  • 15
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.

user3016087
  • 69
  • 1
  • 2
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.

Abhijeet Kashnia
  • 12,290
  • 8
  • 38
  • 50
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

Hardzsi
  • 41
  • 1
  • 3
-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.

Noon Silk
  • 54,084
  • 6
  • 88
  • 105