moved OSIV examples from spring-data-jpa-3 to spring-data-jpa-4

This commit is contained in:
fejera 2019-11-29 14:00:35 +01:00
parent e1dfff0c68
commit 41b17f7d61
16 changed files with 95 additions and 17 deletions

View File

@ -9,8 +9,8 @@
- [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 Springs 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:

View File

@ -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) {

View File

@ -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<Customer, Long>{
}

View File

@ -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) {

View File

@ -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<BasicUser, Long> {
import org.springframework.data.jpa.repository.JpaRepository;
@EntityGraph(attributePaths = "permissions")
Optional<BasicUser> findDetailedByUsername(String username);
import com.baeldung.model.BasicUser;
public interface UserRepository extends JpaRepository<BasicUser, Long> {
Optional<BasicUser> findSummaryByUsername(String username);

View File

@ -1,6 +1,7 @@
### 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 Springs 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:

View File

@ -15,6 +15,11 @@
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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;

View File

@ -0,0 +1,2 @@
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:h2:mem:jpa3