Работа с DB (H2, Spring Data JPA)
2.1 Spring Data JPA. ApplicationRunner
Apply patch 2_01_data_jpa.patch
- Spring Data
- Common application Data properties
- ApplicationRunner and CommandLineRunner
- Проблема N+1. Стратегии загрузки коллекций
- Hibernate configurations
- Open Session In View Anti-Pattern. spring.jpa.open-in-view
2.2 H2. Популирование и конфигурирование
Apply patch 2_02_h2_init.patch
Внимание: если у вас версия spring-boot 2.5.0 и выше, добавьте в application.yaml:
spring.jpa.defer-datasource-initialization: true
H2 Консоль
# ImMemory url: jdbc:h2:mem:voting tcp: jdbc:h2:tcp://localhost:9092/mem:voting # Absolute path url: jdbc:h2:C:/projects/bootjava/restorant-voting/db/voting tcp: jdbc:h2:tcp://localhost:9092/C:/projects/bootjava/restorant-voting/db/voting # Relative path form current dir url: jdbc:h2:./db/voting # Relative path from home url: jdbc:h2:~/voting tcp: jdbc:h2:tcp://localhost:9092/~/voting
2.3 Рефакторинг model
. Spring Data JPA @Query
Apply patch 2_03_model_query.patch
- hashCode/equals в Entity (не делайте сравнение по всем полям!)
- Field vs property access
- Spring Data JPA @Query
- Как работает Spring Data JPA
- Query Creation
- Использование @Param, @RequestParam и @PathVariable