What needs to be taken care of while working on JPA in Java?
Quick tips on JPA for Java
Introduction
Jakarta Persistence or JPA, also formerly known as Java Persistent API, is an application programming interface that manages all your database-related configuration and functionality and provides an easy way to interact with relational data. At the enterprise level developers performs a lot of coding, where interacting with the database is a major challenge, here JPA plays an important role to ease out their work.
But even it is so easy, it makes developers cry sometimes. There are certain procedures and points to be noted facts, that if not taken care of, might make you fall in great trouble. There are a few of them that are discussed below.
Try not to use primitive data types while declaring variables
While using JPA we interact with the database, entity classes play an important role. These classes are those class that maps your table to it and its (table's) column as an instance variable of the entity class.
The point while writing entity class we should keep in mind is, we must always try to skip declaring instance variable using primitive data types like int, float, double, etc. instead we must try, as much possible to use their respective wrapper classes like Integer for int, Float for float, Double for double, etc.
This habit will benefit in two ways:
1- We will be able to use various predefined functions of these wrapper classes, which are not available with primitive data types. For e.g. Integer class has a method to convert integers to another base i.e. binary, octal, hexadecimal, etc. while int cannot.
2- Major benefit is that, in case there is a null value assigned to that column, to which that instance variable refers to, of any row in the database, it can be assigned to wrapper classes while for primitive datatype it will throw NullPointerException.
Use DTOs instead of Entity classes
Usually what developers make mistake is that in case they want to fetch data of all the columns, they use that entity class object list to store that data, which is being used for mapping the table from which data is being fetched from the database.
But this, as per what I have experienced is considered a bad practice. One must always use a separate DTO (Data Transfer Object) to store data. Even if we need to set projections for all the columns but at the DB server-side, it will fetch only those columns which are mentioned by you. Else if we use an entity class object it has to fetch that table data as well as the data of the table or entity class associated with our former table or entity class. This will lead to a decrease in the performance of service.
Where to implement our business logic in business code or database?
This problem generally is faced by new developers who have entered the enterprise level very recently. So, here it means that we can perform many business logic at the database level which may increase the performance of our code. Only thing is that we need to choose quite smartly where our logic must be placed. Here I am discussing a very weak example just to understand.
For e.g. If we want to fetch all the Admins details of the given list of Id’s we can simply use IN clause of SQL instead of fetching individual employee detail one by one.
Conclusion
A little amount of attention may increase the performance issue of your code. This must be the prior goal of any developer. This was mine till date experience which I have shared with you. Hope I can get more similar quick tips in the future.
Comments
There are no comments for this story
Be the first to respond and start the conversation.