The Advantages of using database abstraction with the Java Persistence API (JPA)
The Java Persistence API is a core API that forms the basic building block of different implementations of this API which include Hibernate and OpenJPA. These technologies are not necessarily ground-breaking as they are simply the latest in a long line of database abstraction technologies for the platform. Database abstraction is, in my opinion, something that any software developer who works with a database should learn to understand and utilize. Regular SQL, in its various forms, is not particularly well suited to be used from within an object-oriented context, as database normalization, which is generally a desirable trait in a database's architecture, means that pulling in a set of data usually means having to pull from rows in numerous tables. Using regular SQL, this is usually done using join queries or by sprinkling database calls throughout the code, as needed, to pull in bits and pieces of a record-set, as the code needs them.
To a developer who has any architectural sense or security knowledge, it quickly becomes apparent why the method of using straight SQL calls becomes either inefficient, dangerous, or both. While the issue of security deserves its own attention, suffice it to say that the use of regular SQL, within an application that is taking user input, creates potential security risks of SQL injection attacks and that the headache needed to make sure that they are prevented, itself, should be reason enough not to use regular SQL calls within a web application. From an architectural perspective, having to write extra code that converts results from SQL into an object-oriented data structure also creates headaches and takes away time from actually building the parts of the application that are specific to the business goals.
It is likely that if a team of developers begins trying to use regular SQL within an application, while addressing the issues above, that they would most likely begin building a layer of an application that would be attempting to resemble features already available within the Java Persistence API. Therefore, rather than re-invent the wheel, the decision to use the Java API should come naturally. JPA provides a simple, intuitive way to interact with a SQL database without having to code to the specific database, so that, for example, an application that starts out using MySQL could easily be changed to use PostgreSQL with only minor changes to configuration files.
JPA allows database tables to be mapped to Java classes, with annotations being offered that help streamline the process. Rather than forcing a developer to concoct long-winded join queries, JPA essentially does this for you. All the developer has to do is define relationships, using annotations, within the table Java classes and you have the power of full data-sets at your fingertips. For example, you simply retrieve an object representing an entry from one database table, based on parameters passed in, and then you are able to call getter methods, where you would normally have foreign-key fields, to pull in the an actual object, or collection of objects, that are associated with the initial entry. After getting used to this, it can be hard to imagine ever going back to using straight SQL.
Beyond what is mentioned above, JPA has a number of useful features that are too complex to get into here in detail, including caching of queries and lazy initialization. Both of these features are meant to enable configuration to save on resource usage. Caching enables saving on calls to the database which reduces strain placed on the DB and increases application performance. Lazy initialization also saves on unnecessary queries, while preserving application RAM usage, by only retrieving an associated data-set object when it is called from within the application code.
Hopefully the info above has helped give you an overview of why the JPA is useful. Obviously this isn't a technical tutorial of the nuts and bolts of getting started with the JPA, there are plenty of those to be found. You need to select the "flavor" of JPA you want to use, which could include Hibernate or OpenJPA, and once you do that you can find a variety of tutorials and resources to get started.