/JPA

Primary LanguageJava

JPA – Java persistance API – it is a tool that allows programmers to map Java objects onto database tables and operate on them with objective approach instead of SQL.

1.Entity Creation

image

@Entiy means its JPA entity

@Table – it is mapped on Author table in database

@Id – it is a key

@GeneratedValue – 1,2,3…

image

@ManyToOne(cascade) – means that CRUD operation will also effect bounded Author entity in database

@JoinColumn – joins with id

  1. Hibernate DDL -Hibernate is ORM(Object Relational Mappin) framework for Java. It is used to map Java object onto Database

-Take care of CRUD operations

-Thanks to Hibernate it is easier to change Database

  1. Creating Entities

image

image

  1. Add, FindOne

image

CRUD repository automatically creates CRUD operations

image

Save(), findById() – thats all

  1. Findall, update, delete

Findall:

image

Update:

image

Delete:

image

  1. CustomQueries

image

Thats all… you dont need to do anything, JPA is clever enough to look at authors and get with age less, based just on name of the method

7.HQL

HQL – Hibernate query language. HQL is similiar to SQL but works on Java objects instead of SQL tables

image

We make this name on purpose

image

Select all objects marked as a where age is …