2

I don't understand the difference between POJO and DTO in Java. I have read the article here: What is the Difference Between POJO (Plain Old Java Object) and DTO (Data Transfer Object)? . But I still don't understand the code implementation between them, what makes them different. Can you give the code example for each of them? Thank you so much before!

3 Answers 3

1

POJO means Plain Old Java Object. A POJO is an object that doesn't need to implement any specific interface or extend some specific class to be used by a framework. The term has been coined, if I remember correctly, to react against EJB1 and EJB2 which imposed a lot of constraints on the way you had to write beans to make them EJBs. POJO is more a marketing term than a technical term.

DTO means Data Transfer Object. A DTO is an object that is used to transfer data between layers of an application, typically over the network. It often consists in a POJO (i.e. a simple Java class) containing fields and getters.

6
  • So basically, POJO is not transferable between layers and DTO is transferable? Commented Mar 8, 2015 at 8:12
  • Where did I say that?
    – JB Nizet
    Commented Mar 8, 2015 at 8:13
  • No, you didn't say that. I have just want to reconfirm about my understanding. Commented Mar 8, 2015 at 8:14
  • As I said, POJO is not a technical term. It's a marketing term to say: "look! to use my framework, you don't need to implement any specific interface or extend any specific base class". DTO is a technical term. It's used to refer to those objects who exists only to transfer data between layers.
    – JB Nizet
    Commented Mar 8, 2015 at 8:17
  • DTO is not a technical term. Like POJO, it's just a slang word of javaee developers.
    – Everv0id
    Commented Mar 8, 2015 at 8:21
0

A Plain Old Java Object or POJO is a term initially introduced to designate a simple lightweight Java object, not implementing any javax.ejb interface in contrast to Entity beans Now a days , POJO term is used for any simple object with no extra fancy annotation like third party annotations etc

DTO is an object whose responsibility is just to encapsulate data in a value object . It will just have instance members and corresponding getters and setters. Generally they used to represent the row in data store. While pojo include some more methods that doing some business processing on them

0

Actually, POJO is more general. Any class which has private serializable fields, getters and setter for each field and doesnt extend other class or implement an interface may be called POJO.

DTO is an object which uses some data object (for example, JPA entity object) to transfer data to another tier of your application (for example, if entity1 object has reference to other entity2, DTO can replace entity2 reference by its entity2.id.toString or other entity2 field).

1
  • Hmm.. ok.. this is easier to understand. Commented Mar 8, 2015 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.