DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Merge GraphQL Schemas Using Apollo Server and Koa
  • 13 Impressive Ways To Improve the Developer’s Experience by Using AI
  • Managing Data Residency, the Demo
  • Hyperion Essbase Technical Functionality
  1. DZone
  2. Data Engineering
  3. Databases
  4. Method Chaining in POJO/DTO/JPA/Hibernate Persistence Objects

Method Chaining in POJO/DTO/JPA/Hibernate Persistence Objects

Mohan Ambalavanan user avatar by
Mohan Ambalavanan
·
Aug. 22, 11 · News
Like (0)
Save
Tweet
Share
10.89K Views

Join the DZone community and get the full member experience.

Join For Free

In recent years method chaining has become popular in the Java/JEE world. Frameworks that I am aware of, that have used Method Chaining effectively are, Hibernate/JPA,EasyMock/JMock and jQuery. jQuery has revolutionized the JavaScript world by using readable fluent interfaces. Method chaining has been around for years from the Small Talk era. In C++,it is very common practice to use method chaining in getter/setter classes. Let us see how we can apply method chaining to Java POJO classes.

Typical Java POJO Example

Let us consider following daily stock price example,

DailyPriceData.java

public class DailyPriceData {

       private Date transDate;
	private Double open;
	private Double high;
	private Double low;
	private Double close;
	private Double volume;

	public DailyPriceData () {

	}
        public DailyPriceData (Date transDate,
         Double open, Double close)     {
         this.transDate =new Date(transDate.getTime());
         this.open = open;
         this.close = close;
      }
      public Double getClose() {
             return this.close;
      }
      public void setClose(Double close) {
          this.close = close;
      }       
	................./getters and setters
  }

The DailyPriceData class above is the typical way we generate POJO's or the way our IDE's will generate them. The Builder/Converter class that will construct the above object has to use setter methods or a constructor has to be provided. We certainly will not provide a bloated constructor with six arguments. The way we solve this issue generally is, we provide getters/setters and expect the Builder/Converter class to construct object using getter/setters. If we want object to be immutable, then we would break the object finer like Price(open,high,low,close)etc... and provide a constructor with finer nested objects. We will not discuss the later case here, and we will assume we are providing getters and setters in our typical Java POJO way.

Converter/Builder

In the Enterprise Java world, it is quite common to convert Persistence objects to DTOs or JAXB Beans etc.. Also it is quite common for us to write Builder classes which construct/build objects based on certain attributes or from other objects. See the example below how we construct the object below with normal setters,

PriceBuilder.java

DailyPriceData priceData = new DailyPriceData ();
  priceData.setClose(priceBean.getClose());
  priceData.setHigh(priceBean.getHigh());
  priceData.setLow(priceBean.getLow());
  priceData.setVolume(priceBean.getVolume());

POJO using Method Chaining

public class DailyPriceData {
       private Date transDate;
	private Double open;
	private Double high;
	private Double low;
	private Double close;
	private Double volume;

	public DailyPriceData () {

	}
      public Double getOpen() {
		return open;
	}
	public DailyPrice setOpen(Double open) {
		this.open = open;
                return this;
	}
	public Double getHigh() {
		return high;
	}
	public DailyPrice setHigh(Double high) {
		this.high = high;
                return this;
	}
        .......
  }

As you can see the only difference between our classical Java POJO and the class above is ,it returns the object (DailyPrice in our case) that invokes the setter method. Now we will see how we construct the object in our builder class.

 PriceBarData priceData = new PriceBarData().setClose(priceBean.getClose())
                    .setHigh(priceBean.getHigh())
                   .setLow(priceBean.getLow().
                    setVolume(priceBean.getVolume());

One of the major advantages of using this method is  that if you try writing the above code in IDE's like Intellij or Netbeans you would see how fast you would write the code by tabbing :).. Also the technique above will not disturb your Hibernate generators,or Dozer frameworks.

Fluent Interfaces

Martin Fowler wrote an article based on Eric Evans presentation and named interfaces 'that are primarly designed to be readable and to flow' as fluent interfaces. Though, we use method chaining in the above method, it is not readable and flowable as we wish to describe as price().openedAt().closedAt().surgedAt()...etc... We will definitely break our Hibernate or JAXB frameworks,if we are using any fluent interfaces. As he describes in his article, writing a fluent interface requires good bit of thought.

Also you can find this article in my personal Blog Using Method Chaining in POJO/DTO/ Entities

Method chaining Object (computer science) Persistence (computer science)

Opinions expressed by DZone contributors are their own.

Trending

  • Merge GraphQL Schemas Using Apollo Server and Koa
  • 13 Impressive Ways To Improve the Developer’s Experience by Using AI
  • Managing Data Residency, the Demo
  • Hyperion Essbase Technical Functionality

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: