Merge pull request #8270 from fanatixan/bael-16656

BAEL-16656
This commit is contained in:
Josh Cummings 2019-11-30 12:54:11 -07:00 committed by GitHub
commit 5f9aa44685
19 changed files with 99 additions and 25 deletions

View File

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

@ -47,13 +47,11 @@
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
<version>${h2.version}</version>
</dependency>
<!-- Test Dependencies -->
@ -81,7 +79,6 @@
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>

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,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 Springs 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:

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

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

View File

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