A POJO is just a plain, old Java Bean with the restrictions removed. Java Beans must meet the following requirements:
- Default no-arg constructor
- Follow the Bean convention of getFoo (or isFoo for booleans) and setFoo methods for a mutable attribute named foo; leave off the setFoo if foo is immutable.
- Must implement java.io.Serializable
POJO does not mandate any of these. It's just what the name says: an object that compiles under JDK can be considered a Plain Old Java Object. No app server, no base classes, no interfaces required to use.
The acronym POJO was a reaction against EJB 2.0, which required several interfaces, extended base classes, and lots of methods just to do simple things. Some people, Rod Johnson and Martin Fowler among them, rebelled against the complexity and sought a way to implement enterprise scale solutions without having to write EJBs.
Martin Fowler coined a new acronym.
Rod Johnson wrote "J2EE Without EJBs", wrote Spring, influenced EJB enough so version 3.1 looks a great deal like Spring and Hibernate, and got a sweet IPO from VMWare out of it.
Here's an example that you can wrap your head around:
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; }
}
Answer from duffymo on Stack OverflowA POJO is just a plain, old Java Bean with the restrictions removed. Java Beans must meet the following requirements:
- Default no-arg constructor
- Follow the Bean convention of getFoo (or isFoo for booleans) and setFoo methods for a mutable attribute named foo; leave off the setFoo if foo is immutable.
- Must implement java.io.Serializable
POJO does not mandate any of these. It's just what the name says: an object that compiles under JDK can be considered a Plain Old Java Object. No app server, no base classes, no interfaces required to use.
The acronym POJO was a reaction against EJB 2.0, which required several interfaces, extended base classes, and lots of methods just to do simple things. Some people, Rod Johnson and Martin Fowler among them, rebelled against the complexity and sought a way to implement enterprise scale solutions without having to write EJBs.
Martin Fowler coined a new acronym.
Rod Johnson wrote "J2EE Without EJBs", wrote Spring, influenced EJB enough so version 3.1 looks a great deal like Spring and Hibernate, and got a sweet IPO from VMWare out of it.
Here's an example that you can wrap your head around:
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; }
}
POJO:- POJO is a Java object not bound by any restriction other than those forced by the Java Language Specification.
Properties of POJO
- All properties must be public setter and getter methods
- All instance variables should be private
- Should not Extend prespecified classes.
- Should not Implement prespecified interfaces.
- Should not contain prespecified annotations.
- It may not have any argument constructors
Example of POJO
public class POJO {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Why is POJO used in Java?
How do POJO classes work with Hibernate?
Is a POJO always serializable?
Videos
Yes, it's possible to generate simple POJOs from Database Tables in IntelliJ.
Just follow this instruction:
- Connect your database to IntelliJ.
- Right click the tables you want to generate your POJOs for
- Right Click on your selection --> Scripted Extensions --> Generate POJOs.groovy
- Select where to save your POJO classes
- Press OK and your POJO files will be created.
Hope this helps. - Tested on IntelliJ 2019.1
It is still possible, but in newer versions you need to have "POJO Generator" plugin installed, then:
- Open database View | Tool Windows | Database
- In the Database tool window ( View | Tool Windows | Database) , click the Data Source Properties button
- In the Database tool window ( View | Tool Windows | Database) , click the Add button, choose you db type
- Here you fill host, username/pass and etc.
- Right click the tables you want to generate your POJOs for and you will see 'POJO generator' at the bottom of the list
- Choose Entity and place in project where to save it
Use Lombok.
You can annotate your class, for example:
@Data //generate getters and setters
@EqualsAndHashCode(callSuper=true) //self descriptive
@NoArgsConstructor //self descriptive
@AllArgsConstructor //self descriptive
Remark: it works only with Eclipse for now.
I have just used Practical macros, within a few minutes of install from the market place, I could generate *constructors*, getters / setters, toString, hashcode and equals (basically chaining the standard eclipse commands) in a single command. Just what I was looking for and saved me loads of time. I can also see a lot more uses for it, well done to Earnst (the creator).