What is the difference between a JavaBean and a POJO?

JavaTerminologyPojo

Java Problem Overview


I'm not sure about the difference. I'm using Hibernate and, in some books, they use JavaBean and POJO as an interchangeable term. I want to know if there is a difference, not just in the Hibernate context, but as general concepts.

Java Solutions


Solution 1 - Java

A JavaBean follows certain conventions. Getter/setter naming, having a public default constructor, being serialisable etc. See JavaBeans Conventions for more details.

A POJO (plain-old-Java-object) isn't rigorously defined. It's a Java object that doesn't have a requirement to implement a particular interface or derive from a particular base class, or make use of particular annotations in order to be compatible with a given framework, and can be any arbitrary (often relatively simple) Java object.

Solution 2 - Java

All JavaBeans are POJOs but not all POJOs are JavaBeans.

A JavaBean is a Java object that satisfies certain programming conventions:

  • the JavaBean class must implement either Serializable or Externalizable;
  • the JavaBean class must have a public no-arg constructor;
  • all JavaBean properties must have public setter and getter methods (as appropriate);
  • all JavaBean instance variables should be private.

Solution 3 - Java

According to Martin Fowler a POJO is an object which encapsulates Business Logic while a Bean (except for the definition already stated in other answers) is little more than a container for holding data and the operations available on the object merely set and get data.

> The term was coined while Rebecca Parsons, Josh MacKenzie and I were > preparing for a talk at a conference in September 2000. In the talk we > were pointing out the many benefits of encoding business logic into > regular java objects rather than using Entity Beans. 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. > > http://www.martinfowler.com/bliki/POJO.html

Solution 4 - Java

Pojo - Plain old java object

pojo class is an ordinary class without any specialties,class totally loosely coupled from technology/framework.the class does not implements from technology/framework and does not extends from technology/framework api that class is called pojo class.

pojo class can implements interfaces and extend classes but the super class or interface should not be an technology/framework.

Examples :

class ABC{
----
}

ABC class not implementing or extending from technology/framework that's why this is pojo class.

class ABC extends HttpServlet{
---
}

ABC class extending from servlet technology api that's why this is not a pojo class.

class ABC implements java.rmi.Remote{
----
}

ABC class implements from rmi api that's why this is not a pojo class.

class ABC implements java.io.Serializable{
---
}

this interface is part of java language not a part of technology/framework.so this is pojo class.

class ABC extends Thread{
--
}

here thread is also class of java language so this is also a pojo class.

class ABC extends Test{
--
}

if Test class extends or implements from technologies/framework then ABC is also not a pojo class because it inherits the properties of Test class. if Test class is not a pojo class then ABC class also not a pojo class.

now this point is an exceptional case

@Entity
class ABC{
--
}

@Entity is an annotation given by hibernate api or jpa api but still we can call this class as pojo class. class with annotations given from technology/framework is called pojo class by this exceptional case.

Solution 5 - Java

POJO: If the class can be executed with underlying JDK,without any other external third party libraries support then its called POJO

JavaBean: If class only contains attributes with accessors(setters and getters) those are called javabeans.Java beans generally will not contain any bussiness logic rather those are used for holding some data in it.

All Javabeans are POJOs but all POJO are not Javabeans

Solution 6 - Java

> Java beans are special type of POJOs.

Specialities listed below with reason

enter image description here

Solution 7 - Java

In summary: similarities and differences are:

   java beans:							Pojo:
-must extends serializable 				-no need to extends or implement.
 or externalizable.						
-must have public class .				- must have public class
-must have private instance variables.		-can have any access specifier variables.
-must have public setter and getter method. - may or may not have setter or getter method.
-must have no-arg constructor.			 - can have constructor with agruments.

All JAVA Beans are POJO but not all POJOs are JAVA Beans.

Solution 8 - Java

POJOS with certain conventions (getter/setter,public no-arg constructor ,private variables) and are in action(ex. being used for reading data by form) are JAVABEANS.

Solution 9 - Java

You've seen the formal definitions above, for all they are worth.

But don't get too hung up on definitions. Let's just look more at the sense of things here.

JavaBeans are used in Enterprise Java applications, where users frequently access data and/or application code remotely, i.e. from a server (via web or private network) via a network. The data involved must therefore be streamed in serial format into or out of the users' computers - hence the need for Java EE objects to implement the interface Serializable. This much of a JavaBean's nature is no different to Java SE application objects whose data is read in from, or written out to, a file system. Using Java classes reliably over a network from a range of user machine/OS combinations also demands the adoption of conventions for their handling. Hence the requirement for implementing these classes as public, with private attributes, a no-argument constructor and standardised getters and setters.

Java EE applications will also use classes other than those that were implemented as JavaBeans. These could be used in processing input data or organizing output data but will not be used for objects transferred over a network. Hence the above considerations need not be applied to them bar that the be valid as Java objects. These latter classes are referred to as POJOs - Plain Old Java Objects.

All in all, you could see Java Beans as just Java objects adapted for use over a network.

There's an awful lot of hype - and no small amount of humbug - in the software world since 1995.

Solution 10 - Java

All Pojo(s) are JavaBean(s), but not the opposite.


What is POJO?

  1. A POJO has no naming convention for properties or methods. We don't follow any real convention for constructing, accessing, modifying the class's state.

    Example:

     public class Pojo {
    
      public String firstname;
      public String LASTName;
    
      public String name() {
         return this.firstname + " " + this.LASTName;
      }
     }
    

    here, I could have replaced firstname by first_name or Firstname or by any noun and the same with the variable LASTName.

> The term has most likely gained widespread acceptance because of the > need for a common and easily understood term that contrasts with > complicated object frameworks.[2]


Reflection using POJO.

> it may limit a framework's ability to favor convention over configuration, understand how to use the class, and augment its functionality.[1]

  List<String> propertyNames =
                Arrays.stream(PropertyUtils.getPropertyDescriptors(Pojo.class))
                        .map(PropertyDescriptor::getDisplayName)
                        .collect(Collectors.toList());
        System.out.println(propertyNames);

If we use Third Party Libraries PropertyUtils for reflection we may face issues, as this will result in

[]

What is Java Beans?

> A JavaBean is still a POJO but introduces a strict set of rules around > how we implement it: > > - Access levels – our properties are private and we expose getters and setters.
> - Method names – our getters and setters follow the getX and setX convention (in the case of a boolean, isX can be used for a getter)
> - Default Constructor – a no-argument constructor must be present so an instance can be created without providing arguments, for example during deserialization
> - Serializable – implementing the Serializable interface allows us to store the state.

Example:

@Getter
@Setter
 class Pojo implements Serializable {
    public String firstName;
    public String lastName;
}

Reflection using Java Bean.

If we again use third party Libraries such as PropertyUtils for reflection the result will be different

[firstName,lastName]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDJphilomathView Question on Stackoverflow
Solution 1 - JavaBrian AgnewView Answer on Stackoverflow
Solution 2 - JavanagapavanView Answer on Stackoverflow
Solution 3 - JavaSphygmomanometerView Answer on Stackoverflow
Solution 4 - JavaMandeep GillView Answer on Stackoverflow
Solution 5 - Javaprasad paluruView Answer on Stackoverflow
Solution 6 - JavaJeril KuruvilaView Answer on Stackoverflow
Solution 7 - Javaishab acharyaView Answer on Stackoverflow
Solution 8 - Javauser4768611View Answer on Stackoverflow
Solution 9 - JavaTrunkView Answer on Stackoverflow
Solution 10 - JavaZahid KhanView Answer on Stackoverflow