34-Spring Boot - Spring Data Flashcards
What is the starter for Spring JPA?
spring-boot-starter-data-jpa
What jars are inside Spring JPA?
- spring-boot-starter.jar
- spring-boot-starter-jdbc.jar
- spring-boot-starter-aop.jar
- spring-data-jpa.jar
- hibernate-core.jar
- javax.transaction-api
What happens if Spring boot found JPA on the classpath?
It will auto-configure:
- DataSource
- LocalContainerEntityManagerFactoryBean
- JpaTransactionManager
How to customize EntityManager?
Configure the bean LocalContainerEntityManagerFactoryBean and set:
- JpaVendorAdapter e.g. HibernateJpaVendorAdapter
- JpaProperties e.g. hibernate.format_sql
- Datasource
- PackagesToScan
What is the purpose of @EnableAutoConfiguration? Does it belong to spring framework or spring boot?
- Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need.
Auto-configuration classes are usually applied based on your classpath and what beans you have defined - Part of Spring Boot
What beans will be scanned with @EnableAutoConfiguration?
- Package of the config class annotated with @EnableAutoConfiguration
- All sub-packages
In Spring Boot, how to customize the packages that will be scanned for entities?
Using @EntityScan e.g.
@SpringBootApplication
@EntityScan(“my.package”)
public class Application {}
Spring JPA: how to customize DB name?
spring.jpa.database=my-db-name
Spring JPA: how to disable the feature that creates tables automatically?
spring.jpa.hibernate.ddl-auto=none
What are options for spring.jpa.hibernate.ddl-auto? What is the default one?
none validate update create create-drop (default)
How to show SQL queries in a nice format?
spring. jpa.show-sql=true
spring. jpa.properties.hibernate.format_sql=true
How to set a custom property for Hibernate?
spring.jpa.properties.hibernate.xxx=…
What ORMs does Spring support?
Hibernate only
What are the benefits of using Spring data?
- Reduce boilerplate code for data access
2. Works in many environments
List out 6 sub-projects of Spring Data
- JPA
- MongoDB
- Redis
- Neo4j
- Hadoop
- Solr
Motivation for Spring Data
- Provide similar support for NoSQL databases that Spring does for RDBMS
a. Template classes to hide low-level
b. Common data-access exceptions
c. Portability of the code over different storage technologies - It implements repositories for you
How to define a repository?
- Annotate domain class with @Entity, (optional: @Table, @Id, @GeneratedValue)
- Define your repository as an interface
What interfaces will be scanned for repositories?
Interfaces that extend Repository or CrudRepository
How to define an auto-increment id?
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
What is the annotation for domain class in MongoDB and Neo4j, Gemfire?
@Document - MongoDb
@NodeEntity - Neo4J
@Region - Gemfire
List out methods in Repository
It’s empty
List out 5 methods in CrudRepository
- save(T): T
- saveAll(Iterable): Iterable
- findById(ID), existsById(ID), findAll(): Iterable, findAllById(Iterable)
- delete(T), deleteById(ID), deleteAll(Iterable)
- count()
Describe PagingAndSortingRepository
- Extend CrudRepository
- Add 2 more methods:
Iterable findAll(Sort)
Page findAll(Pageable)
Ways to implement the method to get the first 10 records?
- findFirst10ByField(field)
2. findTop10ByField(field)