Java Persistence API Introduction (Part 1)

DZone 's Guide to

Java Persistence API Introduction (Part 1)

In the first of a series on persisting objects in Java, we explore the world of JPA and how it can help you persist your objects into a database.

· Java Zone ·
Free Resource

In this two-part article, I explain that business POJOs 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.

As is typical with Java EE there are various implementations of JPA available from EclipseLink, TopLink, and Hibernate ORM, but I won’t be looking at those in this article.

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.

Further Reading

How about learning a little about the following Java EE technologies:

java ee ,java ,orm ,jpa ,tutorial

Published at DZone with permission of Alex Theedom , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}