As i know, all JavaBeans are POJOs but all POJOs are JavaBeans is not true
What is java bean:
A JavaBean is a Java object that satisfies certain programming conventions:
Definition of a Bean
1. Class must implement either Serializable or Externalizable;
2. Class must have a no-arg constructor;
3. All properties must have public setter and getter methods (as appropriate) ;
4. Should be variables private.
example
@Entity
public class Employee implements Serializable{
@Id
private int id;
private String name;
private int salary;
public Employee() {}
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId( int id ) {
this.id = id;
}
public int getSalary() {
return salary;
}
public void setSalary( int salary ) {
this.salary = salary;
}
}
Definition of POJO: The Plain Old Java Object
If you have been familiar with C/C++ you might know what a struct is. A struct (from structure) is an arrangement or a grouping of several items (e.g. int, float, double, char[]) in a memory block for the ease of access and addressing. This concept allows a group of parameters to be passed with a single pointer (or reference). And that reduces the access time to the parameters (kinda of a little private cache). Struct is useful and applied as a frame for file (record), or for a DB-Table (row).
Because JAVA does not include everything from C/C++, also struct isn’t a part of JAVA so that any file record or DB-table processing is a nightmare, a terror for every JAVA newbie. The usage of an object as a single parameter is not new within JAVA development community. BUT: there’s no rule, no convention to “standardize” an object as parameter. POJO was introduced in 2000 and it simply follows the C/C++ struct
As you see the transformation from a struct to a POJO is a straight-forward task. Because POJO is also a JAVA class it might have its own methods, too. And these methods are usually called the Getters and/or the Setters.
Properties of POJO.
1. All properties must public setter and getter methods
2. All instance variables should be private
3. Should not Extend prespecified classes.
4. Should not Implement prespecified interfaces.
5. Should not contain prespecified annotations.
6. It may not have no argument constructor
public class MyFirstPojo
{
private String name;
public static void main(String [] args)
{
for (String arg : args)
{
MyFirstPojo pojo = new MyFirstPojo(arg); // Here's how you create a POJO
System.out.println(pojo);
}
}
public MyFirstPojo(String name)
{
this.name = name;
}
public String getName() { return this.name; }
public String toString() { return this.name; }
}
So, from point of my view:
“A Javabean must have a no-arg constructor and implement either Serializable or Externalizable, but pojo is not necessary (don’t have these restrictions.)”
I’m looking forward to getting your feedback!
(cont.)