JPA and Spring Data JPA
The Java Persistence API is a standard technology that lets you “map” objects to relational
databases. The spring-boot-starter-data-jpa POM provides a quick way to get started. It provides
the following key dependencies:
• Hibernate: One of the most popular JPA implementations.
• Spring Data JPA: Makes it easy to implement JPA-based repositories.
• Spring ORMs: Core ORM support from the Spring Framework.
We do not go into too many details of JPA or Spring Data here. You can follow the
“Accessing Data with JPA” guide from spring.io and read the Spring Data JPA and
Hibernate reference documentation.
not necessary and “Entity Scanning” is used instead. By default, all packages below your main
configuration class (the one annotated with @EnableAutoConfiguration or @SpringBootApplication)
are searched.
Any classes annotated with @Entity, @Embeddable, or @MappedSuperclass are considered. A typical
entity class resembles the following example:
package com.example.myapp.domain;
import java.io.Serializable;
import javax.persistence.*;
@Entity
public class City implements Serializable {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String state;
// ... additional members, often include @OneToMany mappings
protected City() {
// no-args constructor required by JPA spec
// this one is protected since it shouldn't be used directly
}
public City(String name, String state) {
this.name = name;
this.state = state;
}
public String getName() {
return this.name;
}
public String getState() {
return this.state;
}
// ... etc
}
created automatically from your method names. For example, a CityRepository interface might
declare a findAllByState(String state) method to find all the cities in a given state.
For more complex queries, you can annotate your method with Spring Data’s Query annotation.
Spring Data repositories usually extend from the Repository or CrudRepository interfaces. If you use
auto-configuration, repositories are searched from the package containing your main configuration
class (the one annotated with @EnableAutoConfiguration or @SpringBootApplication) down.
The following example shows a typical Spring Data repository interface definition:
package com.example.myapp.domain;
import org.springframework.data.domain.*;
import org.springframework.data.repository.*;
public interface CityRepository extends Repository<City, Long> {
Page<City> findAll(Pageable pageable);
City findByNameAndStateAllIgnoringCase(String name, String state);
}
Spring Data JPA repositories support three different modes of bootstrapping: default, deferred, and
lazy. To enable deferred or lazy bootstrapping, set the
configprop:spring.data.jpa.repositories.bootstrap-mode[] property to deferred or lazy respectively.
When using deferred or lazy bootstrapping, the auto-configured EntityManagerFactoryBuilder will
use the context’s AsyncTaskExecutor, if any, as the bootstrap executor. If more than one exists, the
one named applicationTaskExecutor will be used.
HSQL, or Derby). You can explicitly configure JPA settings by using spring.jpa.* properties. For
example, to create and drop tables you can add the following line to your application.properties:
spring.jpa.hibernate.ddl-auto=create-drop
Hibernate’s own internal property name for this (if you happen to remember it
better) is hibernate.hbm2ddl.auto. You can set it, along with other Hibernate native
properties, by using spring.jpa.properties.* (the prefix is stripped before adding
them to the entity manager). The following line shows an example of setting JPA
properties for Hibernate:
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
The line in the preceding example passes a value of true for the
hibernate.globally_quoted_identifiers property to the Hibernate entity manager.
By default, the DDL execution (or validation) is deferred until the ApplicationContext has started.
There is also a spring.jpa.generate-ddl flag, but it is not used if Hibernate auto-configuration is
active, because the ddl-auto settings are more fine-grained.
OpenEntityManagerInViewInterceptor to apply the “Open EntityManager in View” pattern, to allow
for lazy loading in web views. If you do not want this behavior, you should set spring.jpa.open-inviewto false in your application.properties.
databases. The spring-boot-starter-data-jpa POM provides a quick way to get started. It provides
the following key dependencies:
• Hibernate: One of the most popular JPA implementations.
• Spring Data JPA: Makes it easy to implement JPA-based repositories.
• Spring ORMs: Core ORM support from the Spring Framework.
We do not go into too many details of JPA or Spring Data here. You can follow the
“Accessing Data with JPA” guide from spring.io and read the Spring Data JPA and
Hibernate reference documentation.
Entity Classes
Traditionally, JPA “Entity” classes are specified in a persistence.xml file. With Spring Boot, this file isnot necessary and “Entity Scanning” is used instead. By default, all packages below your main
configuration class (the one annotated with @EnableAutoConfiguration or @SpringBootApplication)
are searched.
Any classes annotated with @Entity, @Embeddable, or @MappedSuperclass are considered. A typical
entity class resembles the following example:
package com.example.myapp.domain;
import java.io.Serializable;
import javax.persistence.*;
@Entity
public class City implements Serializable {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String state;
// ... additional members, often include @OneToMany mappings
protected City() {
// no-args constructor required by JPA spec
// this one is protected since it shouldn't be used directly
}
public City(String name, String state) {
this.name = name;
this.state = state;
}
public String getName() {
return this.name;
}
public String getState() {
return this.state;
}
// ... etc
}
Spring Data JPA Repositories
Spring Data JPA repositories are interfaces that you can define to access data. JPA queries arecreated automatically from your method names. For example, a CityRepository interface might
declare a findAllByState(String state) method to find all the cities in a given state.
For more complex queries, you can annotate your method with Spring Data’s Query annotation.
Spring Data repositories usually extend from the Repository or CrudRepository interfaces. If you use
auto-configuration, repositories are searched from the package containing your main configuration
class (the one annotated with @EnableAutoConfiguration or @SpringBootApplication) down.
The following example shows a typical Spring Data repository interface definition:
package com.example.myapp.domain;
import org.springframework.data.domain.*;
import org.springframework.data.repository.*;
public interface CityRepository extends Repository<City, Long> {
Page<City> findAll(Pageable pageable);
City findByNameAndStateAllIgnoringCase(String name, String state);
}
Spring Data JPA repositories support three different modes of bootstrapping: default, deferred, and
lazy. To enable deferred or lazy bootstrapping, set the
configprop:spring.data.jpa.repositories.bootstrap-mode[] property to deferred or lazy respectively.
When using deferred or lazy bootstrapping, the auto-configured EntityManagerFactoryBuilder will
use the context’s AsyncTaskExecutor, if any, as the bootstrap executor. If more than one exists, the
one named applicationTaskExecutor will be used.
Creating and Dropping JPA Databases
By default, JPA databases are automatically created only if you use an embedded database (H2,HSQL, or Derby). You can explicitly configure JPA settings by using spring.jpa.* properties. For
example, to create and drop tables you can add the following line to your application.properties:
spring.jpa.hibernate.ddl-auto=create-drop
Hibernate’s own internal property name for this (if you happen to remember it
better) is hibernate.hbm2ddl.auto. You can set it, along with other Hibernate native
properties, by using spring.jpa.properties.* (the prefix is stripped before adding
them to the entity manager). The following line shows an example of setting JPA
properties for Hibernate:
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
The line in the preceding example passes a value of true for the
hibernate.globally_quoted_identifiers property to the Hibernate entity manager.
By default, the DDL execution (or validation) is deferred until the ApplicationContext has started.
There is also a spring.jpa.generate-ddl flag, but it is not used if Hibernate auto-configuration is
active, because the ddl-auto settings are more fine-grained.
Open EntityManager in View
If you are running a web application, Spring Boot by default registersOpenEntityManagerInViewInterceptor to apply the “Open EntityManager in View” pattern, to allow
for lazy loading in web views. If you do not want this behavior, you should set spring.jpa.open-inviewto false in your application.properties.
Comments
Post a Comment