JPA - Java Persistance API. we want to take java objects data and store it in DB (tables).
JPA provides a way of mapping from POJO to the database tables. we can use SQL statements/ Criteria API, JPQL to query the database.
Important Annotations - @Entity, @Table(name = "tablename"), @Id @GenerateValue, @Column, @OneToOne, @OneToMany, @ManyToMany.
Useful Notes:
- JPA is an interface/specification - Hibernate(ORM tool) is an implementation of interface.
- EntityManager is used to manage all the Entities. The EntityManager API is used to access a database in a particular unit of work. It is used to create and remove persistent entity instances, to find entities by their primary key identity, and to query over all entities.
- There are many useful methods that we can make use of in EntityManger like persist etc.. More info click here..
- Transaction management can be managed by @Transactional can be used on class level or method level.
- add spring.jpa.show-sql= true, spring.h2.console.enabled=true
- url for h2 - http://localhost:8080/h2-console, use db url jdbc:h2:mem:testdb.
- Lets look whats happening in background - springboot auto configration is doing all the work. as we are using in memory database h2. spring boot initializes the schema. HibernateJpaAutoConfiguration , DataSourceAutoConfiguration, EntityManager, TransactionManager is auto configured.
SPRING DATA JPA provides JpaRespository<EntityClassName, Long> and lot of important methods.