Skip to the content.

Java Persistance API (JPA)

-

Introduction

-

What is Persistence

-

Manipulating Persisted Data Without JPA

-

Java DataBase Connectivity

-

What’s wrong with JDBC?

-

Manipulating Persisted Data With JPA

-

Advantages of JPA

-

Summary

-

Understanding JPA

-

What is a Database?

-

What is a Relational Database?

-

What is Object Oriented Programming?

-

What is Object Relational Mapping?

-

What is Java Persistence API?

-

What isn’t Java Persistence API

-

A Brief of History of JPA

-

JPA Specification

-

JPA Implementations

-

JPA Packages

Package Description
javax.persistence Core API
javax.persistencecriteria Criteria API
javax.persistence.metamodel Metamodel API
javax.persistence.spi SPI for JPA providers

-

Where can we use JPA?

-

A JPA Entity

@Entity
public class Book {
	@id
	private Long id;
	private String title;
	Private String description;
	
	private Float unitCost;
	private String isbn;
	
	// Constructors, getters & setters
}

-

Mapping Metadata

TODO: need some text for below

@Entity
public class Book {
	@id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	@Column(name = "book_title", nullable = false)
	private String title;
	@Column(length = 2000)
	private String description;
	@Column(name="unit_cost")
	private Float unitCost;
	private String isbn;
	
	// Constructors, getters & setters
}

-

CRUD Operations

An EntityManagerFactory instance is obtained by using a static factory method of the JPA bootstrap class, Persistence.

The EntityManagerFactory instance, when constructed, opens the database. If the database does not yet exist, a new database file is created.

-

CRUD Operations(continued)

The createEntityManagerFactory method takes as an argument a name of a persistence unit.

An EntityManager instance may represent either a remote connection to a remote database server (in client-server mode) or a local connection to a local database file (in embedded mode). The functionality in both cases is the same.

-

public class Main{
	public static void main(String ...args){
		static EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPU");
		static EnitityManager em = emf.createEntityManager();
		
		public static void main(String ...args){
			Book book = new Book(4044L, "H2G2", "Scifi Book", 12.5f, "1234-5678-5678, 247);
			em.persist(book);
			
			book = em.find(Book.class, 4044L);
			
			TypedQuery<Book> query = em.createQuery("Select b From Book b order by b.title", Book.class);
			query.getResultList();
	}
}

-

Summary

-

Managing Elementery Entities with JPA

-

Managing Entities