34-Spring Boot - Spring Data Flashcards

1
Q

What is the starter for Spring JPA?

A

spring-boot-starter-data-jpa

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What jars are inside Spring JPA?

A
  1. spring-boot-starter.jar
  2. spring-boot-starter-jdbc.jar
  3. spring-boot-starter-aop.jar
  4. spring-data-jpa.jar
  5. hibernate-core.jar
  6. javax.transaction-api
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What happens if Spring boot found JPA on the classpath?

A

It will auto-configure:

  1. DataSource
  2. LocalContainerEntityManagerFactoryBean
  3. JpaTransactionManager
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to customize EntityManager?

A

Configure the bean LocalContainerEntityManagerFactoryBean and set:

  1. JpaVendorAdapter e.g. HibernateJpaVendorAdapter
  2. JpaProperties e.g. hibernate.format_sql
  3. Datasource
  4. PackagesToScan
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the purpose of @EnableAutoConfiguration? Does it belong to spring framework or spring boot?

A
  1. 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
  2. Part of Spring Boot
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What beans will be scanned with @EnableAutoConfiguration?

A
  1. Package of the config class annotated with @EnableAutoConfiguration
  2. All sub-packages
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In Spring Boot, how to customize the packages that will be scanned for entities?

A

Using @EntityScan e.g.
@SpringBootApplication
@EntityScan(“my.package”)
public class Application {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Spring JPA: how to customize DB name?

A

spring.jpa.database=my-db-name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Spring JPA: how to disable the feature that creates tables automatically?

A

spring.jpa.hibernate.ddl-auto=none

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are options for spring.jpa.hibernate.ddl-auto? What is the default one?

A
none
validate
update
create
create-drop (default)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to show SQL queries in a nice format?

A

spring. jpa.show-sql=true

spring. jpa.properties.hibernate.format_sql=true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to set a custom property for Hibernate?

A

spring.jpa.properties.hibernate.xxx=…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What ORMs does Spring support?

A

Hibernate only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the benefits of using Spring data?

A
  1. Reduce boilerplate code for data access

2. Works in many environments

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

List out 6 sub-projects of Spring Data

A
  1. JPA
  2. MongoDB
  3. Redis
  4. Neo4j
  5. Hadoop
  6. Solr
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Motivation for Spring Data

A
  1. 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
  2. It implements repositories for you
17
Q

How to define a repository?

A
  1. Annotate domain class with @Entity, (optional: @Table, @Id, @GeneratedValue)
  2. Define your repository as an interface
18
Q

What interfaces will be scanned for repositories?

A

Interfaces that extend Repository or CrudRepository

19
Q

How to define an auto-increment id?

A

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

20
Q

What is the annotation for domain class in MongoDB and Neo4j, Gemfire?

A

@Document - MongoDb
@NodeEntity - Neo4J
@Region - Gemfire

21
Q

List out methods in Repository

A

It’s empty

22
Q

List out 5 methods in CrudRepository

A
  1. save(T): T
  2. saveAll(Iterable): Iterable
  3. findById(ID), existsById(ID), findAll(): Iterable, findAllById(Iterable)
  4. delete(T), deleteById(ID), deleteAll(Iterable)
  5. count()
23
Q

Describe PagingAndSortingRepository

A
  1. Extend CrudRepository
  2. Add 2 more methods:
    Iterable findAll(Sort)
    Page findAll(Pageable)
24
Q

Ways to implement the method to get the first 10 records?

A
  1. findFirst10ByField(field)

2. findTop10ByField(field)

25
Q

List out repository query subject keywords

A
  1. (find or read or get or search or stream)…By
    optional: DomainType e.g. findUserBy… = findBy
  2. exists…By
  3. count…By
  4. (remove or delete)…By
  5. …(First or Top)…
  6. …Distinct…
26
Q

List out 6 repository query predicate keywords

A
  1. And, Or
  2. Between, IsBetween
  3. False, IsFalse
  4. GreaterThan, IsGreaterThan
  5. NotNull, IsNotNull
  6. Is, Equals, (or no keyword)
    Notice that most of the operators support 2 forms:
    and Is e.g. In, IsIn
    Read more here: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repository-query-keywords
27
Q

List out repository query predicate modifier keywords

A
  1. IgnoreCase, IgnoringCase
  2. AllIgnoreCase, AllIgnoringCase
  3. OrderBy
28
Q

Create a repository method to search Users by username and email, return data should be sorted by FirstName ascending then LastName descending

A

findByUsernameAndEmailOrderByFirstNameAscLastNameDesc(String username, String email)
Notice that there’s no And after FirstNameAsc

29
Q

How to control repositories scanner manually?

A

@Configuration

@EnableJpaRepository(basePackages=”my.package”)

30
Q

Describe JpaRepository

A
  1. extends PagingAndSortingRepository
  2. Provide flush(), saveAndFlush(T entity)
  3. deleteInBatch, deleteAllInBatch
  4. getOne(ID): access a lazy-loading reference
31
Q

How to create a custom repository method?

A
1. Create a repository extends an automatic repository e.g.
interface CustomerRepository extends Repository {
    User customMethod();
}
2. Option 1: Implement with the class name: CustomerRepositoryImpl (Spring will search for postfix Impl), no need to mark with annotation
Option 2: Implement with any class name then create a bean with the name "CustomerRepositoryImpl"
32
Q

What is the result of MyRepository.findById(id) if not found?

A

null

33
Q

How to make MyRepository.findById(id) return optional object?

A

Declare the return type with Optional: Optional findById(id)

34
Q

How to customize the custom repository scanning (e.g. scan for CustomCustomerRepository implementation)?

A

@Configuration
@EnableJpaRepository(repositoryImplementationPostFix=”Manager”)
default is: “Impl”

35
Q

When will the test with @SpringBootTest scan for a @SpringBootConfiguration?

A

When:

  1. there is no nested @Configuration
  2. no explicit classes are specified (e.g. @SpringBootTest(classes = RewardsConfig.class))
36
Q

Which of the following implementations of PlatformTransactionManager can be used with EclipseLink JPA?

A

JpaTransactionManager

JtaTransactionManager