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 Video Library
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
View Events Video Library
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
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

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Full-Stack Observability Essentials: Explore the fundamentals of system-wide observability and key components of the OpenTelemetry standard.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Build a Serverless App Fast with Zipper: Write TypeScript, Offload Everything Else
  • Training a Handwritten Digits Classifier in Pytorch With Apache Cassandra Database
  • Build a Digital Collectibles Portal Using Flow and Cadence (Part 1)
  • Creating a Deep vs. Shallow Copy of an Object in Java

Trending

  • Agile Frameworks in Action: Enhancing Flexibility in Service Delivery
  • What Technical Skills Can You Expect To Gain From a DevOps Course Syllabus?
  • How to Prompt ChatGPT So It Writes Quality Tech Texts for Your Website
  • Application Integration for IoT Devices: A Detailed Guide To Unifying Your Ecosystem
  1. DZone
  2. Data Engineering
  3. Data
  4. A Portable JPA Boolean Magic Converter

A Portable JPA Boolean Magic Converter

Khoo Chen Shiang user avatar by
Khoo Chen Shiang
·
Apr. 14, 08 · Interview
Like (0)
Save
Tweet
Share
28.14K Views

Join the DZone community and get the full member experience.

Join For Free

The current Java Persistence API (JPA) standard does not mandate JPA provider to support data type conversions through annotations, not even a with simple boolean field. For readers who are unfamiliar with JPA, what I mean is, to persist a boolean field, JPA expects the database data type to be integer, where value of "1" means true, and value of "0" means false.

We simply just can't annotate the boolean field, specifying our own boolean field value, such as "True/False", "T/F", "Yes/No", "Y/N", "-1/0" and then let the JPA provider to convert those boolean field on the fly. As an example, I am expecting JPA will allow me to annotate a boolean field
@boolean(trueValue="Yes", falseValue="No")
private boolean enabled;

For me, this is a very annoying limitation, espeically when you have to deliver applications on an existing database with hundreds of tables.

After some research, I decided to create my own Java Annotation Processing Tool (APT) Compile Time Annotation, called @BooleanMagic, with a compile time Java APT preprocessing factory, which will automatically generate additional code to work around the issue.

I've also made some changes on the original code:

  • Fixed the bug of Null pointer exception, when JPA return null on the annotated field.
  • Introduce a new properties call ifNull, which allows user to configure what to return if JPA returns null, it expect enum of org.jbpcc.util.jpa.ReturnType, which have values of ReturnType.True, ReturnType.FALSE, and ReturnType.Null,. The default value of ifNull is ReturnType.Null

So here is an  example, assuming we have model class defined as below:

package org.jbpcc.domain.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.jbpcc.util.jpa.BooleanMagic;
import org.jbpcc.util.jpa.BooleanMagic.ReturnType;

@Entitypublic class SomeVO
{
@Id private Integer id;
@BooleanMagic(trueValue = "Yes", falseValue = "No",
columnName = "OVERDUED", ifNull = ReturnType.FALSE)
private transient Boolean overdued;
public Boolean isOverdued()
{
return overdued;
}
public void setOverdued(Boolean overdued)
{
this.overdued = overdued;
}
}

Using Java APT with JPABooleanMagicConverter factory, the code above will be now be converted to:

@Entitypublic class SomeVO 
{     
     @Id   private Integer id;   
      private transient Boolean overdued;   
        //--- Lines below are generated by JBPCC BooleanMagicConvertor PROCESSOR   
        //--- START :   
       @Column(name="OVERDUED")   
       private String magicBooleanOverdued;   
       public Boolean isOverdued() 
      {      
         if (this.magicBooleanOverdued == null)          
              return false;       
          return this.magicBooleanOverdued.equals("Yes") ? Boolean.TRUE : Boolean.FALSE;   
      }   
      public Boolean getOverdued() 
     {       
          if (this.magicBooleanOverdued == null)            
               return false;       
             return this.magicBooleanOverdued.equals("Yes") ? Boolean.TRUE : Boolean.FALSE;   
      }   
       public void setOverdued(Boolean trueFlag) 
      {        
          this.magicBooleanOverdued = trueFlag ? "Yes" : "No";   
       }  
      //--- END  
       //--- GENERATED BY JBPCC BooleanMagicConvertor PROCESSOR
 }

I have put the annotation with it compile time process factory under my open source project - Java Batch Process control center, at http://code.google.com/p/jbpcc, I also posted an article at my blog detailing the usage of the annotation. I hope some readers will find the annotation and the APT preprocessing factory useful.

Do share your thoughts and suggestions.

Data Types

Opinions expressed by DZone contributors are their own.

Related

  • Build a Serverless App Fast with Zipper: Write TypeScript, Offload Everything Else
  • Training a Handwritten Digits Classifier in Pytorch With Apache Cassandra Database
  • Build a Digital Collectibles Portal Using Flow and Cadence (Part 1)
  • Creating a Deep vs. Shallow Copy of an Object in Java

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

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: