• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Understanding POJO in Java

 
Greenhorn
Posts: 24
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read somewhere that POJO class should not contain getter setter methods. Below is the quote,

Mutable Data Structure is something which gives you a provision to add edit data in it. so the opposite of data structure is an Object.
Object doesn’t gives you a provision to edit its state (only in extreme conditions you do that, when no options left). Object gives behavior.
Frameworks like JEE frameworks, spring, Hibernate and others gives a false definition of POJO (mixing it with JavaBeans). i.e. a class having private fields and getter setter methods. Basically these are Data structures and not Objects.



I am confused with these statements about using getter and setter methods in my code. Can someone correct my understanding about using POJO's in java code.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have never seen that definition of an object before; please (always) tell us where your quotes come from, so we can assess the information ourselves, and to avoid copyright problems.
 
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regardless of the source, I completely disagree with the quote.
 
Ranch Hand
Posts: 100
2
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A plain old Java object (POJO) is an ordinary Java object, not bound by any special restriction  https://en.wikipedia.org/wiki/Plain_Old_Java_Object
a POJO should not have to extend or implement prespecified classes or contain prespecified annotations


Below is a simple POJO class




A JavaBean is a POJO ( Ordinary Java Object ) that is serializable, has a no-argument constructor, and allows access to properties using getter and setter methods that follow a simple naming convention.
 
shital sonavane
Greenhorn
Posts: 24
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the source for my quotes.. http://novice2wise.blogspot.in/2016/07/understanding-pojo.html
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shital sonavane wrote:Here is the source for my quotes.. http://novice2wise.blogspot.in/2016/07/understanding-pojo.html


OK, well I don't agree with his definition of OOP for starters, particularly the "and extreme late-binding of all things" bit because it basically invalidates Java as an OO language, since most things other than method invocation are NOT late-bound....at least not before version 8 they weren't.

I also don't agree that an Object "hides the state and do not allow to change/expose it"; although I would agree that it's generally preferable (see this article). However, this suggests that the way an object is written determines whether it's a POJO or not, whereas I've always assumed that - as he says later on - a POJO is "an object that isn't tied to a framework".

I.e. - a basic Java object.

My 2¢.

Winston
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that article has changed the definition of a POJO to suit his argument.

A POJO is far more loosely defined.
It's simply a class that can be used pretty much anywhere without the need for an over-arching framework to run it.
So a Servlet is not a POJO (it requires a servlet container to function properly).
Annotations have blurred the lines a bit, it has to be said...

Java Beans are a subset of POJOs, with some tighter restrictions on how they should be written (re: getters/setters and a no-arg constructor).
 
shital sonavane
Greenhorn
Posts: 24
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But in that article he has given reference of Alan Kays Definition Of Object Oriented....
 
M Khalid
Ranch Hand
Posts: 100
2
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shital sonavane wrote:Here is the source for my quotes.. http://novice2wise.blogspot.in/2016/07/understanding-pojo.html



I disagree with many statements on this blog.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can still be wrong when you quote somebody else. Another thing this article says is:-

But when we say it's an object, it gives behavior, it doesn't allows to modify the state unless and until its an extreme condition or required.

“...or required.” ?? What does that mean? It means it only has to change when it has to change. That is a grammatical tautology, which I think vitiates part of that article, with or without the other errors.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I took a look at all the sources the article cited, and none actually say that mutability violates OO. The article draws an incorrect conclusion.
 
M Khalid
Ranch Hand
Posts: 100
2
Python Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alan Kay contributed in Smalltalk

Smalltalk is a "pure" object-oriented programming language, meaning that, unlike Java and C++, there is no difference between values which are objects and values which are primitive types. In Smalltalk, primitive values such as integers, booleans and characters are also objects, in the sense that they are instances of corresponding classes, and operations on them are invoked by sending messages.

for more Reference check this Viewpoints Research Institute
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

M Khalid wrote:Alan Kay contributed in Smalltalk
Smalltalk is a "pure" object-oriented programming language, meaning that, unlike Java and C++, there is no difference between values which are objects and values which are primitive


Which is kind of the way I understand "pure" in that context. However, that doesn't mean to say that all OO languages are necessarily pure - Java isn't by a long chalk; and C# blurs the concept somewhat with the idea of a 'struct' (which, I have to admit, I quite like).

But we seem to be straying into "what is an OO language", rather than "what is a POJO"....

Winston
 
shital sonavane
Greenhorn
Posts: 24
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree with Winston. We are here to discuss what is POJO? And is it good practice to add getters and setters in POJO.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shital sonavane wrote:Agree with Winston. We are here to discuss what is POJO? And is it good practice to add getters and setters in POJO.


I don't agree. It's good practice to make your data private. You add getters and setters only when necessary.
 
M Khalid
Ranch Hand
Posts: 100
2
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shital sonavane wrote:Agree with Winston. We are here to discuss what is POJO? And is it good practice to add getters and setters in POJO.



POJO is an ordinary Java object, it can be without setters and getters. You can use Getters/Setters whenever its useful or necessary.
Like in JavaBeans where it is necessary to use Getters and Setters.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I don't agree. It's good practice to make your data private. You add getters and setters only when necessary.


OK, but is an object that doesn't NOT a POJO? Not in my book.

Sorry for all the double-negatives.

Winston
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By definition of the term POJO, there are no special restrictions on such an object, including whether or not to use setters or getters.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:By definition of the term POJO, there are no special restrictions on such an object, including whether or not to use setters or getters.



Exactly.
It's the set of which Java Beans are a subset.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read about this once. POJO means plain old java object. it doesn't inherit from any class or interface.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randall Twede wrote:. . . POJO . . . doesn't inherit from any class or interface.

Good grief! Where did you read that? It sounds just as wrong as the things in the blog mentioned earlier.
 
Ranch Hand
Posts: 234
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Where did you read that?


Looks like it came from Wikipedia

Wikipedia wrote:Ideally speaking, 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:

  • Extend prespecified classes, as in
    public class Foo extends javax.servlet.http.HttpServlet { ...
  • Implement prespecified interfaces, as in
    public class Bar implements javax.ejb.EntityBean { ...
  • Contain prespecified annotations, as in
    @javax.persistence.Entity public class Baz { ...
  •  
    Daniel Cox
    Ranch Hand
    Posts: 234
    12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It looks like this definition is designed to suit the Spring framework which imposes no restrictions on its objects unlike the Servlet framework and JavaBean specification which impose certain restrictions on their objects.

    Wikipedia wrote:Spring was an early implementation of this idea and one of the driving forces behind popularizing this model.



     
    Sheriff
    Posts: 22781
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That articles says that POJO's don't have to extend a class or implement an interface. It doesn't say they can't.
     
    Daniel Cox
    Ranch Hand
    Posts: 234
    12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rob Spoor wrote:It doesn't say they can't.


    I think it does. It says that a plain old Java object (POJO) is an ordinary Java object not bound by any special restriction. A framework (like EJB 1.1) that forces its objects to extend a class or implement an interface cannot claim to be POJO-centric. A POJO-centric framework does not force its objects to do anything other than the basics, for example, Hibernate insists only on a default constructor.

    Beginning POJOs From Novice to Professional (2006) – Page 111 wrote:Hibernate does not force you to implement any special interfaces or extend any particular class; you can work with clean and simple POJOs.


     
    Marshal
    Posts: 28176
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Daniel Cox wrote:

    Rob Spoor wrote:It doesn't say they can't.


    I think it does.



    So you think that a POJO is not allowed to implement any interface? (That's what "can't" means to me in that context, although "mustn't" would be clearer.)

    Your subsequent Hibernate quote only says that Hibernate doesn't require its objects to implement or extend any interface or class. That isn't the same thing as saying that Hibernate forbids them from doing that. In other words, what Rob Spoor said.
     
    Rob Spoor
    Sheriff
    Posts: 22781
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In fact, Hibernate fully supports inheritance. Just check out @MappedSuperclass or @Inheritance.
     
    Daniel Cox
    Ranch Hand
    Posts: 234
    12
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rob Spoor wrote:That articles says that POJO's don't have to extend a class or implement an interface. It doesn't say they can't.


    I see your point. A POJO is not bound by any special restriction to extend a class or implement an interface; however, this does not mean that it cannot extend a class or implement an interface.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Daniel Cox wrote:I see your point. A POJO is not bound by any special restriction to extend a class or implement an interface; however, this does not mean that it cannot extend a class or implement an interface.


    In fact, if it can't, I'd say that it isn't a Java object.

    My understanding of a POJO is that it's an object that makes sense to your application, not dictated by another - usually for the purposes of object/information mapping.

    So a Customer is a POJO, but a CustomerDTO - or indeed a ResultSet containing rows of "customers" - is not.

    I'm not exactly sure where I'd put Hibernate objects in that definition - probably somewhere in between: They can be used like POJOs, but their structure is often dictated by the table(s) that populate them.

    Winston
     
    Daniel Cox
    Ranch Hand
    Posts: 234
    12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:In fact, if it can't, I'd say that it isn't a Java object.


    I agree. No sensible OO language forbids inheritance.

    Winston Gutkowski wrote:I'm not exactly sure where I'd put Hibernate objects in that definition - probably somewhere in between: They can be used like POJOs, but their structure is often dictated by the table(s) that populate them.


    What makes them POJOs is that they are not aware of the technology that is used to persist them. Yes their structure is dictated by the table(s) used to populate them but they are still just simple Java objects with properties. Unlike EJB 1.1, Hibernate does not insist that they must implement this interface or extend that class in other to be persisted. In other words, what makes them POJOs is that Hibernate is non-invasive.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Daniel Cox wrote:What makes them POJOs is that they are not aware of the technology that is used to persist them.


    Yup, I can live with that.

    Winston
    reply
      Bookmark Topic Watch Topic
    • New Topic