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 |
 | ----- | ---- |
 | _____ | ____ |
Share Comments
comments powered by Disqus