8

May I know what is the difference between POJO (Plain old java object) and DAO (Data access object) in Java.

Where to use which one?

From the Jboss hibernate tutorial (http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch04.html#persistent-classes-pojo), why POJO is used with database if we have DAO?

Thanks in advance

Rajesh Kumar
  • 1,270
  • 4
  • 15
  • 31
  • 3
    A POJO is a simple Java object whilst a DAO is a facade to access the database not directly by SQL or similar but by passing objects at it and the DAO has to handle persisting by itself. – Smutje Aug 12 '14 at 07:01
  • See http://stackoverflow.com/questions/19154202/data-access-object-dao-in-java – hoipolloi Aug 12 '14 at 07:02
  • 3
    POJO means that your class will have all getters and setters for each attribute. DAO will handle data access and modification. A DAO is able to use POJO object in order to save or get one. – Athanor Aug 12 '14 at 07:09
  • DAO = Data Access Object. POJO = Plain Old Java Object. If you just look at the full terms you should already see what their general purpose is. – Gimby Aug 12 '14 at 08:19

2 Answers2

8

In Java parlance, a DAO is an organizing class that contains methods to access a database table. A POJO holds database records. A DAO will return POJOs from some of its methods.

Svante
  • 50,694
  • 11
  • 78
  • 122
6

Term is already easy to understand. It is Design pattern. if you agree it, you can use. Otherwise, you cannot.

As you mention

POJO is Plain old java object which take responsibility to keep the data, not business processing.

DAO is Data access object which take responsibility to process persistence/database processing.

Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131