Java Persistence API (JPA)
In the JDBC examples, did you notice that we were constantly creating one DTO Java object for every row of the table while reading data from the database? Conversely, we were also inserting one row of data into the table for every DTO object. So, effectively, we were creating one Java object to represent one row of table data.
This methodology is routine across all eCommerce solutions, and then came the need to automate this process. In this automated process, we give the DTO object to an API, and the API handles the entire gamut of getting a database connection, creating a statement, getting a result set, etc., that a typical DAO does. Java itself defined such an API, called JPA, and this software architectural layer that does this object-to-relational mapping is called an ORM layer.
There are many software solutions that provide this ORM layer and that implement JPA. The popular ones are:
- Hibernate - Open source and by far the most popular implementation of JPA.
- Spring JPA - A Spring wrapper for JPA.
- Apache Cayenne - Open source and a later entrant.
- TopLink - By Oracle; works well with their WebLogic and other servers.
Using an ORM layer saves you from writing the same old boilerplate, tedious code for converting your Java objects to relational tables and vice versa. Most of these frameworks rely on annotations to convey the relationship.