JPA: Part 1

This post is part of Project Digitisation. I have decided to write about JPA in several posts. This is part 1 of the series.

JPA – Java Persistence API

JPA is a specification. There are several implementations like Hibernate, TopLink etc. Persisting objects in DB is an integral part of Enterprise applications.
JPA provides:

  • ORM (Object Relation Mapping) i.e. it maps an Object to a Relation(Table).
    • An Object which can be mapped is called an Entity
  • JPQL – Java Persistence Query Language

Entity classes should be Java beans (POJOs with private variables and public getters and setters).

Annotations used:

  • @Entity
    • Used before a class name. By default, table name is same as class name.
  • @Id
    • Used before the instance variable which is the primary key in the table.

By default instance variable names is same as the column names.

Example:

@Entity
public class Employee {
    @Id
    private int empId; //becomes primary key
    private String name;

    //getters and setters
}

this maps to
Table: Employee

 ----------------
 | empId | name |
 ----------------
 |_______|______|
Advertisements

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s