This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Object Relational Mapping and the fly likes What is this POJO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "What is this POJO" Watch "What is this POJO" New topic
Author

What is this POJO

Kamal Mettananda
Ranch Hand

Joined: Oct 25, 2005
Posts: 51

In a project I was using DAO and EJB (session), while it was called that the project was using POJO.
But I could not really see something different than using DAO pattern.

Would you be able to ellaberate why this POJO word is there.


Kamal Mettananda - SCJP 1.4 (98%)
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
POJO : Plain Old Java Objects
but depending on the context, one may have referred EJBs also as POJOs
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by Yilmaz Mete:
POJO : Plain Old Java Objects
but depending on the context, one may have referred EJBs also as POJOs


If you did, you would be making a mistake. EJBs need the container to run, POJOs don't, so EJBs are not Plain java objects.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

In the EJB 3.0 version all bean classes will be POJO.


Groovy
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

...unless of course you are talking about Session EJBs. Or Message Driven EJBs.
[ January 24, 2006: Message edited by: Paul Sturrock ]
Kamal Mettananda
Ranch Hand

Joined: Oct 25, 2005
Posts: 51

No I'm not querying about EJB.

My question is why there is a concept like POJO? Anyway from the very begging of Java we were using Java objects but now those Java objects are given the name as POJO. But it's just the same Java objects.

So are we using a new word without saying just Java Objects?
Manoj Kumkumath
Ranch Hand

Joined: Dec 01, 2005
Posts: 71
Kamal,
Googling gave me this
POJO

Looks like �What's is there in a name� has became deprecated.
Kamal Mettananda
Ranch Hand

Joined: Oct 25, 2005
Posts: 51

Mmm.. Great Manoj, it has something like this.

"We wondered why people were so against using regular objects in their systems and concluded that it was because simple objects lacked a fancy name. So we gave them one, and it's caught on very nicely."


If that's just talking about Java Objects do we have to think anything more? It's just like learning J2SE. Is that correct?
Fintan Conway
Ranch Hand

Joined: Apr 03, 2002
Posts: 141
Originally posted by Kamal Mettananda:
If that's just talking about Java Objects do we have to think anything more? It's just like learning J2SE. Is that correct?


Hi Kamal,

In the beginning there were only ordinary Java objects. Then along came EJBs and the like, which are more complicated Java objects. Everybody seemed to jump on the EJB bandwagon, and all objects created for an enterprise system were being created as EJBs. In order to differentiate between the ordinary objects and EJBs, the term POJO was created to refer to "ordinary" Java objects, i.e. non-EJB objects.

You are right that POJOs are written in J2SE, and do not include J2EE concepts like servlets, EJBs, etc., nor does a POJO include graphic concepts like AWT, Swing which are included in J2SE. A POJO is an object which simply contains data and methods, and nothing fancier.

Regards,

Fintan
Kamal Mettananda
Ranch Hand

Joined: Oct 25, 2005
Posts: 51

Hi Fintan,

So there are two types, POJO and EJB.

A POJO is an object which simply contains data and methods, and nothing fancier.


Ok good. But there's still a word for those saying Java Beans which is used to express about the objects that contain data and getters and setters.

So the main difference between a Java Bean and a POJO is that it has some business methods other than the getters and setters.

That is Java Bean is a subset of POJO..

Hope this is correct.
Manoj Kumkumath
Ranch Hand

Joined: Dec 01, 2005
Posts: 71

A POJO is an object which simply contains data and methods, and nothing fancier.


I think what Fintan mean is that POJO doesn't use any fancy stuff like container for getting thing done. I think we can not say that we use/don't use POJO at class level. It should be taked in a higher lever like how our application is persisting the data to db. Is it using plain java classes or Is it using fancy stuff like an CM persistence.
Mike Firkser
Ranch Hand

Joined: Oct 21, 2003
Posts: 247

To put it in laymen's terms, POJO is just using basic Java - not getting fancy. Am I right?


Mike Firkser
Rutgers '84
Macy
Greenhorn

Joined: Oct 28, 2005
Posts: 26
I never heard POJO, I would like to find out more about it.


Macy Wong<br />SCJP 1.4 & 5.0, SCJD, SCWCD, SCBCD, Oracle PL/SQL OCA
Chris Richardson
author
Ranch Hand

Joined: Jan 10, 2006
Posts: 50
Hello,

See the post I made in http://www.coderanch.com/t/216003/ORM/java/Chris-Richardson-do-you-call that discusses the definition of a POJO.

The big idea behind POJOs is separation of concerns. So rather than write code that implements business logic and infrastructure concerns such as transactions, security etc, you separate it out.

Your POJOs implement the business logic. They are written using simple Java without implement infrastructure framework-specific interfaces or calling framework-specific APIs.

You then use non-invasive frameworks such as Spring, Hibernate and EJB3 to provide persistence, transactions et etc.

The benefit of this approach is that you can develop and test your business logic without worrying (so much) about application server, the database and the infrastructure frameworks. As a result, development is simpler and faster. You can focus on one thing at a time. You don't have to be an infrastructure framework expert to work on the code. Moreover, you can also switch/upgrade frameworks without having to rewrite your code.

POJOs and JavaBeans are two orthogonal concepts. A JavaBean doesn't have to a POJO and a POJO does not have to follow the JavaBean naming conventions. In fact, well-designed objects should not expose their fields via getters and setters unless it really is required. But that's a whole other thread.

Chris
[ January 24, 2006: Message edited by: Chris Richardson ]

Enterprise Java consulting and training - <a href="http://www.chrisrichardson.net" target="_blank" rel="nofollow">http://www.chrisrichardson.net</a> Author, POJOs in Action - <a href="http://www.manning.com/crichardson" target="_blank" rel="nofollow">http://www.manning.com/crichardson</a> Enterprise POJOs blog - <a href="http://chris-richardson.blog-city.com" target="_blank" rel="nofollow">http://chris-richardson.blog-city.com</a>
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

You then use non-invasive frameworks such as Spring, Hibernate and EJB3 to provide persistence, transactions et etc.


Doesn't using annotations invasive ?
Chris Richardson
author
Ranch Hand

Joined: Jan 10, 2006
Posts: 50
Pradip

Yes. Annotations are invasive. See my message about this topic in this thread: http://www.javaranch.com

Chris
Kamal Mettananda
Ranch Hand

Joined: Oct 25, 2005
Posts: 51

Hi Chris,

According to ur explanation it is clear that java objects are called POJO when they use the basic J2SE level rather than using Framework specific classes or jars. And they basically target on the business logic.

So is it correct to say that POJO is used in the DAO Pattern where the objects are independent of the DBMS?

But one thing is not that clear for me.


Originally posted by Chris Richardson:
POJOs and JavaBeans are two orthogonal concepts.


I feel that both are a bit similar compared to EJB as non of those use any framework specific parts, except that Java Beans are used to store data while POJO are used to store data & business logic.
[ January 25, 2006: Message edited by: Kamal Mettananda ]
Billy Tsai
Ranch Hand

Joined: May 23, 2003
Posts: 1297
POJO=PO?
PO is Persistence Object


BEA 8.1 Certified Administrator, IBM Certified Solution Developer For XML 1.1 and Related Technologies, SCJP, SCWCD, SCBCD, SCDJWS, SCJD, SCEA,
Oracle Certified Master Java EE 5 Enterprise Architect
Chris Richardson
author
Ranch Hand

Joined: Jan 10, 2006
Posts: 50
Originally posted by Billy Tsai:
POJO=PO?
PO is Persistence Object


Some POJOs (Plain Object Java Objects) are persistent where as others are not. e.g. an entity such as Account could be persistent where as a service such as MoneyTransferService is not.

Chris
Chris Richardson
author
Ranch Hand

Joined: Jan 10, 2006
Posts: 50
Originally posted by Kamal Mettananda:
Hi Chris,

According to ur explanation it is clear that java objects are called POJO when they use the basic J2SE level rather than using Framework specific classes or jars. And they basically target on the business logic.



My book POJOs in Action is about the backend (business and database access tiers) but the idea of POJO also applies to the presentation tiers. e.g. POJOs are not coupled to the Servlet API where as non-POJOs are. Compare, for example, WebWork actions with Struts Actions.

Originally posted by Kamal Mettananda:
So is it correct to say that POJO is used in the DAO Pattern where the objects are independent of the DBMS?


DTOs are typically POJOs but they are not particularly interesting objects since they contain only data, no behavior.

Originally posted by Kamal Mettananda:
But one thing is not that clear for me.

I feel that both are a bit similar compared to EJB as non of those use any framework specific parts, except that Java Beans are used to store data while POJO are used to store data & business logic.



JavaBeans that are used as DTOs are typically POJOs as I mentioned above. However, strictly speaking:
* JavaBeans - a naming convention
* DTO - the role of an object
* POJO - a class that does not implement special interfaces

Chris
[ January 25, 2006: Message edited by: Chris Richardson ]
Billy Tsai
Ranch Hand

Joined: May 23, 2003
Posts: 1297
Assuming I have POJO for persistence purpose and one POJO inherits an class ( extends) and that POJO has one to many relationship with another POJO do u think the POJO on the many side should automatically extends the same class too?
Chris Richardson
author
Ranch Hand

Joined: Jan 10, 2006
Posts: 50
Only if makes sense in your object model.

Chris
Kamal Mettananda
Ranch Hand

Joined: Oct 25, 2005
Posts: 51

Hi Chris,

This is quite interesting.


Originally posted by Chris:
* POJO - a class that does not implement special interfaces


So I understand that Plain in POJO really means it's plain.
sinasi susam
Ranch Hand

Joined: Jul 15, 2005
Posts: 67
"Plain old"

I think the chriteria to explain the stuation is they are the simplest objects to store and delegate the records that is related...

"Plain" and "old" like we begin to write java classes at first.Plain and old when we were beginner.I think those words have chosen because sun and everybody else are developing other complex things to persist datas.

Before EJB 3.0 there was too much arguments to persist the datas..
I think "plain old" is an answer such as "no need to complexity.No needed to implement too much complex interfaces and classes.)
mayur dhawan
Greenhorn

Joined: Sep 20, 2005
Posts: 29
POJO's are called Plain Old Java Objects. They are very similar to the regular java objects we use.Its just a name to differentiate it from EJB's
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What is this POJO