Thursday, September 15, 2016

5 Reasons to Use JPA / Hibernate

Before we dive into the reasons to use JPA, let me quickly explain what it is. The Java Persistence API (JPA) is a specification for object-relational mapping in Java. As for most standards within the Java Community Process, it is implemented by different frameworks. The most popular one is Hibernate.

All JPA implementations support the features defined by the specification and often extend that with custom functionality. This provides 2 main advantages:

  1. You can quickly switch your JPA implementation, as long as you’re not using any proprietary features.
  2. The different implementations can add additional features to innovate faster than the standard. Some of them might become part of the specification at a later point in time.

OK, enough theory. Let's start with a short introduction to JPA and then have a look at some reasons to use it.

Getting Started with JPA

It’s, of course, impossible to explain JPA in all its depth in just one short section. But I want to show you a basic use case to make you familiar with the general concepts.

Lets begin with the persistence.xml file. Its structure is defined by the JPA standard and it provides the configuration to the persistence provider, first and foremost the database driver and connection information. You can see a simple example configuration in the following code snippet.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://ift.tt/1cKbVbQ" xmlns:xsi="http://ift.tt/ra1lAU" version="2.1" xsi:schemaLocation="http://ift.tt/1cKbVbQ http://ift.tt/1kMb4sd">
    <persistence-unit name="my-persistence-unit">
        <description>My Persistence Unit</description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.generate_statistics" value="true" />

            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/test" />
            <property name="javax.persistence.jdbc.user" value="postgres" />
            <property name="javax.persistence.jdbc.password" value="postgres" />
        </properties>
    </persistence-unit>
</persistence>

After you have configured your persistence provider, you can define your first entity. The following code snippet shows an example of a simple entity mapping.

@Entity
public class Author {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @Version
    @Column(name = "version")
    private int version;

    @Column
    private String firstName;

    @Column
    private String lastName;

    @ManyToMany(mappedBy="authors")
    private Set<Book> books = new HashSet<Book>();

    // constructors, getters/setters,
    // and everything else is as usual
}

The @Entity annotation defines the Author class as an entity. It gets mapped to a table with the same name, in this case the author table.

The id attribute is the primary key of the entity and database table. The JPA implementation automatically generates the primary key value and uses the version attribute for optimistic locking to avoid concurrent updates of the same database record.

The @Column annotation specifies that this attribute is mapped to a database column. Similar to the @Entity annotation, it uses the name of the attribute as the default column name.

The @ManyToMany annotation defines a relationship to another entity. In this example, it defines the relationship to the Book entity which is mapped to another database table.

As you can see, you only need to add a few annotations to map a database table and use other features like optimistic locking and primary key generation.

5 Reasons

Continue reading %5 Reasons to Use JPA / Hibernate%


by Thorben Janssen via SitePoint

No comments:

Post a Comment