moved OSIV examples from spring-data-jpa-3 to spring-data-jpa-4
This commit is contained in:
parent
e1dfff0c68
commit
41b17f7d61
|
@ -9,8 +9,8 @@
|
||||||
- [Spring Data JPA Batch Inserts](https://www.baeldung.com/spring-data-jpa-batch-inserts)
|
- [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)
|
- [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)
|
- [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)
|
- [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
|
### Eclipse Config
|
||||||
After importing the project into Eclipse, you may see the following error:
|
After importing the project into Eclipse, you may see the following error:
|
||||||
|
|
|
@ -2,8 +2,10 @@ package com.baeldung;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@EnableJpaRepositories
|
||||||
public class Application {
|
public class Application {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package com.baeldung.boot.daos;
|
package com.baeldung.boot.daos;
|
||||||
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import com.baeldung.boot.domain.Customer;
|
import com.baeldung.boot.domain.Customer;
|
||||||
|
|
||||||
|
@ -10,6 +12,8 @@ import com.baeldung.boot.domain.Customer;
|
||||||
* @author ysharma2512
|
* @author ysharma2512
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
@Repository
|
||||||
|
@Transactional
|
||||||
public interface CustomerRepository extends CrudRepository<Customer, Long>{
|
public interface CustomerRepository extends CrudRepository<Customer, Long>{
|
||||||
|
|
||||||
}
|
}
|
|
@ -4,7 +4,6 @@ import java.net.URISyntaxException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
@ -22,7 +21,6 @@ import com.baeldung.boot.domain.Customer;
|
||||||
@RestController
|
@RestController
|
||||||
public class CustomerController {
|
public class CustomerController {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
CustomerRepository customerRepository;
|
CustomerRepository customerRepository;
|
||||||
|
|
||||||
public CustomerController(CustomerRepository customerRepository2) {
|
public CustomerController(CustomerRepository customerRepository2) {
|
|
@ -1,15 +1,12 @@
|
||||||
package com.baeldung.repository;
|
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;
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface UserRepository extends JpaRepository<BasicUser, Long> {
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
@EntityGraph(attributePaths = "permissions")
|
import com.baeldung.model.BasicUser;
|
||||||
Optional<BasicUser> findDetailedByUsername(String username);
|
|
||||||
|
public interface UserRepository extends JpaRepository<BasicUser, Long> {
|
||||||
|
|
||||||
Optional<BasicUser> findSummaryByUsername(String username);
|
Optional<BasicUser> findSummaryByUsername(String username);
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Derived Query Methods in Spring Data JPA Repositories](https://www.baeldung.com/spring-data-derived-queries)
|
- [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)
|
- [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)
|
||||||
|
|
||||||
### Eclipse Config
|
### Eclipse Config
|
||||||
After importing the project into Eclipse, you may see the following error:
|
After importing the project into Eclipse, you may see the following error:
|
||||||
|
|
|
@ -15,6 +15,11 @@
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
|
|
@ -2,8 +2,12 @@ package com.baeldung;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@EnableJpaRepositories
|
||||||
|
@EntityScan
|
||||||
public class Application {
|
public class Application {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
|
@ -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<String> 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<String> getPermissions() {
|
||||||
|
return permissions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPermissions(Set<String> permissions) {
|
||||||
|
this.permissions = permissions;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<BasicUser, Long> {
|
||||||
|
|
||||||
|
@EntityGraph(attributePaths = "permissions")
|
||||||
|
Optional<BasicUser> findDetailedByUsername(String username);
|
||||||
|
|
||||||
|
}
|
|
@ -1,18 +1,19 @@
|
||||||
package com.baeldung.service;
|
package com.baeldung.service;
|
||||||
|
|
||||||
import com.baeldung.model.BasicUser;
|
import java.util.Optional;
|
||||||
import com.baeldung.repository.UserRepository;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.Optional;
|
import com.baeldung.model.BasicUser;
|
||||||
|
import com.baeldung.repository.BasicUserRepository;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class SimpleUserService implements UserService {
|
public class SimpleUserService implements UserService {
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
private final BasicUserRepository userRepository;
|
||||||
|
|
||||||
public SimpleUserService(UserRepository userRepository) {
|
public SimpleUserService(BasicUserRepository userRepository) {
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
package com.baeldung.osiv;
|
package com.baeldung.osiv;
|
||||||
|
|
||||||
|
import com.baeldung.Application;
|
||||||
import com.baeldung.model.BasicUser;
|
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.AfterEach;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
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.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
@ -21,11 +23,12 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@AutoConfigureMockMvc
|
@AutoConfigureMockMvc
|
||||||
|
@ContextConfiguration(classes = Application.class)
|
||||||
@ActiveProfiles("test")
|
@ActiveProfiles("test")
|
||||||
class UserControllerIntegrationTest {
|
class UserControllerIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserRepository userRepository;
|
private BasicUserRepository userRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
|
@ -0,0 +1,2 @@
|
||||||
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
|
spring.datasource.url=jdbc:h2:mem:jpa3
|
Loading…
Reference in New Issue