What is POJO?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • varun aggarwal
    New Member
    • Feb 2007
    • 26

    What is POJO?

    Hi Guys,
    I am going new to enterprise development in java.
    But quite frustrated with this term POJO.
    Will anybody be kind enough to explain what actually is POJO.
    What i am getting from the reading that it is the bean that has getter or setter methods.
    Will it mean that every POJO is infact an "entity"(or entity bean as in ejb 2.x) in ejb3.0.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by varun aggarwal
    Hi Guys,
    I am going new to enterprise development in java.
    But quite frustrated with this term POJO.
    Will anybody be kind enough to explain what actually is POJO.
    What i am getting from the reading that it is the bean that has getter or setter methods.
    Will it mean that every POJO is infact an "entity"(or entity bean as in ejb 2.x) in ejb3.0.
    Where were you reading that? What other things have you looked at and did you do any google and wikipedia searches?

    Comment

    • varun aggarwal
      New Member
      • Feb 2007
      • 26

      #3
      Originally posted by sicarie
      Where were you reading that? What other things have you looked at and did you do any google and wikipedia searches?
      Yes i am doing it . But there is no proper answer to them.
      What only thing i am getting is that these are the entity beans.
      Can anyone give me an exmple of a POJO?

      Comment

      • Kirit
        New Member
        • Mar 2007
        • 2

        #4
        Originally posted by varun aggarwal
        Yes i am doing it . But there is no proper answer to them.
        What only thing i am getting is that these are the entity beans.
        Can anyone give me an exmple of a POJO?
        Actually, a POJO is a class that is NOT an entity bean. POJO stands for Plain Old Java Object, which if taken literally, refers to any java class. However, the implied meaning is a java class that does not extend any class. All entity beans extend the javax.ejb.Entit yBean interface, so they are not POJOs. A pojo that might represent a telephone contact might look like:

        Code:
        public class PhoneRecord{
          String name;
          String phoneNumber;
        }
        Notice how the class doesnt implement or extend anything. It is therefore a "plain old" java object. Typically in enterprise java development, you would write pojos as helper classes that support your business logic.

        Generally, although ther are exceptional cases, a pojo will not extend any other class or implement any interfaces.

        Comment

        • varun aggarwal
          New Member
          • Feb 2007
          • 26

          #5
          Originally posted by Kirit
          Actually, a POJO is a class that is NOT an entity bean. POJO stands for Plain Old Java Object, which if taken literally, refers to any java class. However, the implied meaning is a java class that does not extend any class. All entity beans extend the javax.ejb.Entit yBean interface, so they are not POJOs. A pojo that might represent a telephone contact might look like:

          [CODE]public class PhoneRecord{
          String name;
          String phoneNumber;
          }/[CODE]

          Notice how the class doesnt implement or extend anything. It is therefore a "plain old" java object. Typically in enterprise java development, you would write pojos as helper classes that support your business logic.

          Generally, although ther are exceptional cases, a pojo will not extend any other class or implement any interfaces.
          Hi thanks a lot for this code. I am much clearer about them now.
          Could you provide me a sample program where they are useful and how to use them.?

          Comment

          • brehman
            New Member
            • Jun 2008
            • 1

            #6
            Notice how the class doesnt implement or extend anything. It is therefore a "plain old" java object. Typically in enterprise java development, you would write pojos as helper classes that support your business logic.

            Generally, although ther are exceptional cases, a pojo will not extend any other class or implement any interfaces.

            Hi,

            I dont agree with your statement abou POJOs. If POJO doesnt implement any thing then why we implement an interface serializable in a persistance class with hibernate. for example look at the code below.

            Code:
            import java.io.Serializable;
            
            import javax.persistence.Column;
            import javax.persistence.Entity;
            import javax.persistence.Id;
            import javax.persistence.Table;
            
            @Entity
            @Table(name = "employee")
            public class Employee implements Serializable {
              public Employee() {
            
              }
              @Id
              @Column(name = "id")
              Integer id;
            
              @Column(name = "name")
              String name;
            
              public Integer getId() {
                return id;
              }
            
              public void setId(Integer id) {
                this.id = id;
              }
            
              public String getName() {
                return name;
              }
            
              public void setName(String name) {
                this.name = name;
              }
            
            }
            Thanks in advance if you like to comment.

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #7
              I like a healthy debate as much as the next person...but please use code tags and keep the flames to a minimum if you are planning on going there.

              Comment

              • Dooghy
                New Member
                • Aug 2008
                • 1

                #8
                I kind of agree with Kirit; the only thing I don't agree with is that a POJO can extend a class or implement an interface. But the most important thing is that doesn't comply with the Java Bean's rules, i.e. have getter/setter methods for each attribute.

                Still, the POJO term is quite confusing when reading docs from different framework vendors, e.g. Hibernate, Apache Struts etc.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  A POJO isn't a well defined entity; in the old days when Enterprise Beans were
                  supposed to be a one size fits all solution you had to implement interfaces, extend
                  from other mysterious classes an jump throuh hoops just to make your Java class
                  an Enterprise Bean; and then you hadn't implemented anything useful yet, i.e.
                  all the 'business logic' was still to come.

                  The 'server', some mysterious thing that could handle those Enterprise Beans
                  would create your objects, destroy them or put them to sleep; you had no control
                  over them, or almost no control.

                  People were longing for just Java classes without all the mandatory hulla baloo.
                  Then some other mechanisms saw the light; notably Dependency Injection or
                  also called Inversion of Control allowed you to write just a simple class and the
                  implementation of those patterns took care of the rest, i.e. they 'injected' the
                  context in which such a POJO could operate.

                  And again all those frameworks crept back in again, some wanted you to implement
                  a certain interface or extend from some magic base class in order to get the job
                  done; for convenience the POJO was redefined a bit; sometimes it had to be an
                  ordinary bean, sometimes you had to write wallpaper long xml declarations for
                  them and sometimes those frameworks wanted you to jump through hoops again.

                  And all those classes were called POJOs.

                  kind regards,

                  Jos

                  Comment

                  • shivz
                    New Member
                    • Apr 2014
                    • 1

                    #10
                    a POJO is a Java object not bound by any restriction other than those forced by the Java Language Specification. I.e., a POJO should not have to
                    1.Extend prespecified classes
                    2.implement prespecified interfaces
                    3.contain prespecified annotations..

                    However, due to technical difficulties and other reasons, many software products or frameworks described as POJO-compliant actually still require the use of prespecified annotations for features such as persistence to work properly. The idea is that if the object (actually class) was a POJO before any annotations were added, and would return to POJO status if the annotations are removed then it can still be considered a POJO. Then the basic object remains a POJO in that it has no special characteristics (such as an implemented interface) that makes it a "Specialize d Java Object" (SJO or (sic) SoJO).


                    Hope this helps..

                    Comment

                    • Sherin
                      New Member
                      • Jan 2020
                      • 77

                      #11
                      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 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 no argument constructor

                      For example, this is a POJO:

                      Code:
                      class Point { 
                          private double x; 
                          private double y; 
                      	public double getX() { return x; } 
                          public double getY() { return y; } 
                          public void setX(double v) { x = v; } 
                          public void setY(double v) { y = v; } 
                          public boolean equals(Object other) {...} 
                      }

                      Comment

                      • sarbjitgrewal
                        New Member
                        • Sep 2020
                        • 19

                        #12
                        In Java Enterprise, POJO means Plain Old Java Object. POJO Object in Java is used for the readability and reusability of Java Program.

                        Comment

                        Working...