diff --git a/persistence-modules/spring-data-jpa-3/README.md b/persistence-modules/spring-data-jpa-3/README.md index d3908c3a01..bac52bf114 100644 --- a/persistence-modules/spring-data-jpa-3/README.md +++ b/persistence-modules/spring-data-jpa-3/README.md @@ -9,8 +9,6 @@ - [Spring Data JPA Batch Inserts](https://www.baeldung.com/spring-data-jpa-batch-inserts) - [Batch Insert/Update with Hibernate/JPA](https://www.baeldung.com/jpa-hibernate-batch-insert-update) - [Difference Between save() and saveAndFlush() in Spring Data JPA](https://www.baeldung.com/spring-data-jpa-save-saveandflush) -- [Programmatic Transaction Management in Spring](https://www.baeldung.com/spring-programmatic-transaction-management) -- [A Guide to Spring’s Open Session In View](https://www.baeldung.com/spring-open-session-in-view) ### Eclipse Config After importing the project into Eclipse, you may see the following error: diff --git a/persistence-modules/spring-data-jpa-3/pom.xml b/persistence-modules/spring-data-jpa-3/pom.xml index 99df250a9a..351279f60e 100644 --- a/persistence-modules/spring-data-jpa-3/pom.xml +++ b/persistence-modules/spring-data-jpa-3/pom.xml @@ -47,13 +47,11 @@ org.postgresql postgresql - ${postgresql.version} com.h2database h2 runtime - ${h2.version} @@ -81,7 +79,6 @@ org.junit.platform junit-platform-launcher - ${junit-platform.version} test diff --git a/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/Application.java b/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/Application.java index c0490d50c6..ce10072031 100644 --- a/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/Application.java +++ b/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/Application.java @@ -2,8 +2,10 @@ package com.baeldung; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication +@EnableJpaRepositories public class Application { public static void main(String[] args) { diff --git a/persistence-modules/spring-data-jpa-3/src/test/java/com/baeldung/boot/daos/CustomerRepository.java b/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/boot/daos/CustomerRepository.java similarity index 66% rename from persistence-modules/spring-data-jpa-3/src/test/java/com/baeldung/boot/daos/CustomerRepository.java rename to persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/boot/daos/CustomerRepository.java index 2f1af6ac55..7cb7e45b27 100644 --- a/persistence-modules/spring-data-jpa-3/src/test/java/com/baeldung/boot/daos/CustomerRepository.java +++ b/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/boot/daos/CustomerRepository.java @@ -1,6 +1,8 @@ package com.baeldung.boot.daos; import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; import com.baeldung.boot.domain.Customer; @@ -10,6 +12,8 @@ import com.baeldung.boot.domain.Customer; * @author ysharma2512 * */ +@Repository +@Transactional public interface CustomerRepository extends CrudRepository{ } diff --git a/persistence-modules/spring-data-jpa-3/src/test/java/com/baeldung/boot/web/controllers/CustomerController.java b/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/boot/web/controllers/CustomerController.java similarity index 94% rename from persistence-modules/spring-data-jpa-3/src/test/java/com/baeldung/boot/web/controllers/CustomerController.java rename to persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/boot/web/controllers/CustomerController.java index 69a1e5507e..e13afd9b45 100644 --- a/persistence-modules/spring-data-jpa-3/src/test/java/com/baeldung/boot/web/controllers/CustomerController.java +++ b/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/boot/web/controllers/CustomerController.java @@ -4,7 +4,6 @@ import java.net.URISyntaxException; import java.util.Arrays; import java.util.List; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @@ -22,7 +21,6 @@ import com.baeldung.boot.domain.Customer; @RestController public class CustomerController { - @Autowired CustomerRepository customerRepository; public CustomerController(CustomerRepository customerRepository2) { diff --git a/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/repository/UserRepository.java b/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/repository/UserRepository.java index c656055751..f683be402f 100644 --- a/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/repository/UserRepository.java +++ b/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/repository/UserRepository.java @@ -1,15 +1,12 @@ package com.baeldung.repository; -import com.baeldung.model.BasicUser; -import org.springframework.data.jpa.repository.EntityGraph; -import org.springframework.data.jpa.repository.JpaRepository; - import java.util.Optional; -public interface UserRepository extends JpaRepository { +import org.springframework.data.jpa.repository.JpaRepository; - @EntityGraph(attributePaths = "permissions") - Optional findDetailedByUsername(String username); +import com.baeldung.model.BasicUser; + +public interface UserRepository extends JpaRepository { Optional findSummaryByUsername(String username); diff --git a/persistence-modules/spring-data-jpa-4/README.md b/persistence-modules/spring-data-jpa-4/README.md index 931455224f..63aec5c46b 100644 --- a/persistence-modules/spring-data-jpa-4/README.md +++ b/persistence-modules/spring-data-jpa-4/README.md @@ -1,6 +1,8 @@ ### Relevant Articles: - [Derived Query Methods in Spring Data JPA Repositories](https://www.baeldung.com/spring-data-derived-queries) - [LIKE Queries in Spring JPA Repositories](https://www.baeldung.com/spring-jpa-like-queries) +- [A Guide to Spring’s Open Session In View](https://www.baeldung.com/spring-open-session-in-view) +- [Programmatic Transaction Management in Spring](https://www.baeldung.com/spring-programmatic-transaction-management) ### Eclipse Config After importing the project into Eclipse, you may see the following error: diff --git a/persistence-modules/spring-data-jpa-4/pom.xml b/persistence-modules/spring-data-jpa-4/pom.xml index 2d06b52134..c74119c1f7 100644 --- a/persistence-modules/spring-data-jpa-4/pom.xml +++ b/persistence-modules/spring-data-jpa-4/pom.xml @@ -15,6 +15,11 @@ + + org.springframework.boot + spring-boot-starter-web + + org.springframework.boot spring-boot-starter-data-jpa diff --git a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/Application.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/Application.java index 37dbe7dab8..76d8fe9f87 100644 --- a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/Application.java +++ b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/Application.java @@ -2,8 +2,12 @@ package com.baeldung; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication +@EnableJpaRepositories +@EntityScan public class Application { public static void main(String[] args) { diff --git a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/model/BasicUser.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/model/BasicUser.java new file mode 100644 index 0000000000..2dc9c18cf6 --- /dev/null +++ b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/model/BasicUser.java @@ -0,0 +1,42 @@ +package com.baeldung.model; + +import javax.persistence.*; +import java.util.Set; + +@Entity +@Table(name = "users") +public class BasicUser { + + @Id + @GeneratedValue + private Long id; + + private String username; + + @ElementCollection + private Set permissions; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public Set getPermissions() { + return permissions; + } + + public void setPermissions(Set permissions) { + this.permissions = permissions; + } +} diff --git a/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/model/Payment.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/model/Payment.java similarity index 100% rename from persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/model/Payment.java rename to persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/model/Payment.java diff --git a/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/repository/BasicUserRepository.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/repository/BasicUserRepository.java new file mode 100644 index 0000000000..0912759eb3 --- /dev/null +++ b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/repository/BasicUserRepository.java @@ -0,0 +1,19 @@ +package com.baeldung.repository; + +import java.util.Optional; + +import org.springframework.data.jpa.repository.EntityGraph; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import com.baeldung.model.BasicUser; + +@Repository +@Transactional +public interface BasicUserRepository extends JpaRepository { + + @EntityGraph(attributePaths = "permissions") + Optional findDetailedByUsername(String username); + +} diff --git a/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/service/SimpleUserService.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/service/SimpleUserService.java similarity index 74% rename from persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/service/SimpleUserService.java rename to persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/service/SimpleUserService.java index 4bd1dc5e41..ab7a316129 100644 --- a/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/service/SimpleUserService.java +++ b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/service/SimpleUserService.java @@ -1,18 +1,19 @@ package com.baeldung.service; -import com.baeldung.model.BasicUser; -import com.baeldung.repository.UserRepository; +import java.util.Optional; + import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.Optional; +import com.baeldung.model.BasicUser; +import com.baeldung.repository.BasicUserRepository; @Service public class SimpleUserService implements UserService { - private final UserRepository userRepository; + private final BasicUserRepository userRepository; - public SimpleUserService(UserRepository userRepository) { + public SimpleUserService(BasicUserRepository userRepository) { this.userRepository = userRepository; } diff --git a/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/service/UserService.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/service/UserService.java similarity index 100% rename from persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/service/UserService.java rename to persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/service/UserService.java diff --git a/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/web/DetailedUserDto.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/web/DetailedUserDto.java similarity index 100% rename from persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/web/DetailedUserDto.java rename to persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/web/DetailedUserDto.java diff --git a/persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/web/UserController.java b/persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/web/UserController.java similarity index 100% rename from persistence-modules/spring-data-jpa-3/src/main/java/com/baeldung/web/UserController.java rename to persistence-modules/spring-data-jpa-4/src/main/java/com/baeldung/web/UserController.java diff --git a/persistence-modules/spring-data-jpa-3/src/test/java/com/baeldung/osiv/UserControllerIntegrationTest.java b/persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/osiv/UserControllerIntegrationTest.java similarity index 87% rename from persistence-modules/spring-data-jpa-3/src/test/java/com/baeldung/osiv/UserControllerIntegrationTest.java rename to persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/osiv/UserControllerIntegrationTest.java index 19fdf118ae..a3add5a577 100644 --- a/persistence-modules/spring-data-jpa-3/src/test/java/com/baeldung/osiv/UserControllerIntegrationTest.java +++ b/persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/osiv/UserControllerIntegrationTest.java @@ -1,7 +1,8 @@ package com.baeldung.osiv; +import com.baeldung.Application; import com.baeldung.model.BasicUser; -import com.baeldung.repository.UserRepository; +import com.baeldung.repository.BasicUserRepository; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -9,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.web.servlet.MockMvc; import java.util.Arrays; @@ -21,11 +23,12 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest @AutoConfigureMockMvc +@ContextConfiguration(classes = Application.class) @ActiveProfiles("test") class UserControllerIntegrationTest { @Autowired - private UserRepository userRepository; + private BasicUserRepository userRepository; @Autowired private MockMvc mockMvc; diff --git a/persistence-modules/spring-data-jpa-3/src/test/java/com/baeldung/tx/ManualTransactionIntegrationTest.java b/persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/tx/ManualTransactionIntegrationTest.java similarity index 94% rename from persistence-modules/spring-data-jpa-3/src/test/java/com/baeldung/tx/ManualTransactionIntegrationTest.java rename to persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/tx/ManualTransactionIntegrationTest.java index 1ce8e91ec5..954ff3baf6 100644 --- a/persistence-modules/spring-data-jpa-3/src/test/java/com/baeldung/tx/ManualTransactionIntegrationTest.java +++ b/persistence-modules/spring-data-jpa-4/src/test/java/com/baeldung/tx/ManualTransactionIntegrationTest.java @@ -77,7 +77,7 @@ class ManualTransactionIntegrationTest { }); assertThat(entityManager - .createQuery("select p from Payment p") + .createQuery("select p from Payment p", Payment.class) .getResultList()).isEmpty(); } @@ -104,7 +104,7 @@ class ManualTransactionIntegrationTest { } assertThat(entityManager - .createQuery("select p from Payment p") + .createQuery("select p from Payment p", Payment.class) .getResultList()).isEmpty(); } @@ -122,7 +122,7 @@ class ManualTransactionIntegrationTest { }); assertThat(entityManager - .createQuery("select p from Payment p") + .createQuery("select p from Payment p", Payment.class) .getResultList()).hasSize(1); } @@ -145,7 +145,7 @@ class ManualTransactionIntegrationTest { } assertThat(entityManager - .createQuery("select p from Payment p") + .createQuery("select p from Payment p", Payment.class) .getResultList()).hasSize(1); } diff --git a/persistence-modules/spring-data-jpa-4/src/test/resources/application-test.properties b/persistence-modules/spring-data-jpa-4/src/test/resources/application-test.properties new file mode 100644 index 0000000000..f9497c8f37 --- /dev/null +++ b/persistence-modules/spring-data-jpa-4/src/test/resources/application-test.properties @@ -0,0 +1,2 @@ +spring.jpa.hibernate.ddl-auto=update +spring.datasource.url=jdbc:h2:mem:jpa3 \ No newline at end of file