Introduction to JPA
In this article two-part article, I explain that business POJO’s can be configured to be database entities managed by a persistence context which provides powerful interactions via the Java Persistence API (JPA). Entities can be mapped and modeled via the appropriate use of annotations and there are various ways that the data store can be queried. I will introduce you to the small ecosystem that has grown up around JPA and what it offers.
Data Access Layer
We are at the data access layer and will look at how we can make an application communicate with a database. We want to be able to define a data structure and persist and retrieve data to and from the database. JPA is a powerful API that provides a wide range of services that allows us to do just that.
What is JPA?
Well, it’s an object relational mapping (ORM) framework solution. It is quite a high-level API which means that all you have to do is create your business entities and annotate them with the appropriate domain modeling and relationship mapping annotations and you have yourself an entity mapped to a database. JPA will take care of the handwork of actually making the mappings work for you.
It is worth mentioning that Bean Validation can be used on these entities too. I won’t go into that in this article as I have another article that covers this topic in more detail.
How to Annotate a POJO?
To start you use the @Entity annotation. This goes at the class level and identifies the POJO as an entity ready for mapping. You can then identify the fields that have relationships with other entities and identify their relationships as one-to-one, one-to-many and so on. The following annotations are used to identify these relationships:
- @Entity, @Id, @GeneratedValue, and @Embedded
- @OneToOne, @OneToMany, @ManyToOne, and @ManyToMany
Colum identifiers can be auto generated by annotating the identity column @Id and @GeneratedValue.
You continue by identifying fields in the POJO that relate to the fields in the database table and annotate them appropriate according to their type.
- @Table, and @Embeddable
By default, all fields are mapped to the database unless annotated @Transient. The property type determines the eventual database type but you can specify that it is an enumerated type, a time/date type and so on.
- @Column, @Enumerated, @Temperal, @Transient
Join columns and tables can be identified also.
- @JoinColumn, @JoinTable
JPA Introduction Part 2
In part 2 of this two-part article introducing JPA, I talk about how to query a data store using JPQL and the Criteria API. I also introduce the plugin eco-system.
How about learning a little about the following Java EE technologies:
- Context and Dependency Injection (CDI)
- Enterprise Java Beans (EJB)
- JSP, JSP and Expression Language
- RESTful web services
If you are new to Java EE it can be overwhelming to get your head around all the APIs the form that enterprise eco-system. That is why I wrote and recorded the video training course Learning Java Enterprise Edition. It is a two-hour course that introduces you to all the most important Java EE APIs. With plenty of demonstrations, code examples, and practice tasks on how to program with Enterprise Java, you will up to speak and well on your way to being a Java EE developer.
Advance Your Knowledge
If you want to learn more, there are courses that dive deeper into each of the APIs. There is a course about the JAX-RS API in you advance your knowledge by learning how to construct RESTful endpoints. There is a course on the WebSocket API where you can learn how to develop a chat application and there is a course about JSON where you learn how to master the JSON-Processing API course. There are many courses on the horizon, so why not jump in now and give your Java EE career a kick.