Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is meaning of Plain Old Java Object (POJO)?

What does the term Plain Old Java Object (POJO) mean? I couldn't find anything explanatory enough.

POJO's Wikipedia page says that POJO is an ordinary Java Object and not a special object. Now, what makes or what doesn't make and object special in Java?

The above page also says that a POJO should not have to extend prespecified classes, implement prespecified Interfaces or contain prespecified Annotations. Does that also mean that POJOs are not allowed to implement interfaces like Serializable, Comparable or classes like Applets or any other user-written Class/Interfaces?

Also, does the above policy (no extending, no implementing) means that we are not allowed to use any external libraries?

Where exactly are POJOs used?

EDIT: To be more specific, am I allowed to extend/implement classes/interfaces that are part of the Java or any external libraries?

like image 706
Nikit Batale Avatar asked Jul 24 '10 18:07

Nikit Batale


People also ask

What does POJO mean in Java?

In software engineering, a plain old Java object (POJO) is an ordinary Java object, not bound by any special restriction.

What is meant by POJO in spring?

POJO is short for Plain old java object, an application implemented in pojo way means the logic resides in POJO with little to no boilerplate code, thus it's very portable. An PoJo application can be ported from Spring to another container with little modification.

What is POJO and model class?

The term "POJO" initially denoted a Java object which does not follow any of the major Java object models, conventions, or frameworks. Ideally speaking, a POJO is a Java object not bound by any restriction other than those forced by the Java Language Specification.

What is POJO data model?

A data model is the hierarchy of memory classes that represent the data for the report application. A data model consist of one or more classes. These memory classes are called POJO (plain old java objects). The data model is serialized (converted to XML) to be sent to the Genero Report Engine.


2 Answers

Plain Old Java Object The name is used to emphasize that a given object is an ordinary Java Object, not a special object such as those defined by the EJB 2 framework.

class A {}
class B extends/implements C {}

Note: B is non POJO when C is kind of distributed framework class or ifc. e.g. javax.servlet.http.HttpServlet, javax.ejb.EntityBean or J2EE extn and not serializable/comparable. Since serializable/comparable are valid for POJO.

Here A is simple object which is independent. B is a Special obj since B is extending/implementing C. So B object gets some more meaning from C and B is restrictive to follow the rules from C. and B is tightly coupled with distributed framework. Hence B object is not POJO from its definition.

Code using class A object reference does not have to know anything about the type of it, and It can be used with many frameworks.

So a POJO should not have to 1) extend prespecified classes and 2) Implement prespecified interfaces.

JavaBean is a example of POJO that is serializable, has a no-argument constructor, and allows access to properties using getter and setter methods that follow a simple naming convention.

POJO purely focuses on business logic and has no dependencies on (enterprise) frameworks. It means it has the code for business logic but how this instance is created, Which service(EJB..) this object belongs to and what are its special characteristics( Stateful/Stateless) it has will be decided by the frameworks by using external xml file.

Example 1: JAXB is the service to represent java object as XML; These java objects are simple and come up with default constructor getters and setters.

Example 2: Hibernate where simple java class will be used to represent a Table. columns will be its instances.

Example 3: REST services. In REST services we will have Service Layer and Dao Layer to perform some operations over DB. So Dao will have vendor specific queries and operations. Service Layer will be responsible to call Which DAO layer to perform DB operations. Create or Update API(methods) of DAO will be take POJOs as arguments, and update that POJOs and insert/update in to DB. These POJOs (Java class) will have only states(instance variables) of each column and its getters and setters.

In practice, some people find annotations elegant, while they see XML as verbose, ugly and hard to maintain, yet others find annotations pollute the POJO model. Thus, as an alternative to XML, many frameworks (e.g. Spring, EJB and JPA) allow annotations to be used instead or in addition to XML:

Advantages:
Decoupling the application code from the infrastructure frameworks is one of the many benefits of using POJOs. Using POJOs future proofs your application's business logic by decoupling it from volatile, constantly evolving infrastructure frameworks. Upgrading to a new version or switching to a different framework becomes easier and less risky. POJOs also make testing easier, which simplifies and accelerates development. Your business logic will be clearer and simpler because it won't be tangled with the infrastructure code

References : wiki source2

like image 163
Kanagavelu Sugumar Avatar answered Sep 24 '22 00:09

Kanagavelu Sugumar


According to Martin Fowler, he and some others came up with it as a way to describe something which was a standard class as opposed to an EJB etc.

like image 28
Grant Crofton Avatar answered Sep 21 '22 00:09

Grant Crofton