moved save vs saveAndFlush examples from spring-data-jpa-2 to spring-data-jpa-3
This commit is contained in:
@@ -10,6 +10,5 @@
|
||||
- [Spring Data JPA Delete and Relationships](https://www.baeldung.com/spring-data-jpa-delete)
|
||||
- [Spring Data JPA and Named Entity Graphs](https://www.baeldung.com/spring-data-jpa-named-entity-graphs)
|
||||
|
||||
- [Difference Between save() and saveAndFlush() in Spring Data JPA](https://www.baeldung.com/spring-data-jpa-save-saveandflush)
|
||||
- [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)
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.baeldung.entity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Employee {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
public Employee() {
|
||||
}
|
||||
|
||||
public Employee(Long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.baeldung.entity.Employee;
|
||||
|
||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.entity.Employee;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DataJpaTest
|
||||
public class EmployeeRepositoryIntegrationTest {
|
||||
|
||||
private static final Employee EMPLOYEE1 = new Employee(1L, "John");
|
||||
private static final Employee EMPLOYEE2 = new Employee(2L, "Alice");
|
||||
|
||||
@Autowired
|
||||
private EmployeeRepository employeeRepository;
|
||||
|
||||
@Test
|
||||
public void givenEmployeeEntity_whenInsertWithSave_ThenEmployeeIsPersisted() {
|
||||
employeeRepository.save(EMPLOYEE1);
|
||||
assertEmployeePersisted(EMPLOYEE1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeEntity_whenInsertWithSaveAndFlush_ThenEmployeeIsPersisted() {
|
||||
employeeRepository.saveAndFlush(EMPLOYEE2);
|
||||
assertEmployeePersisted(EMPLOYEE2);
|
||||
}
|
||||
|
||||
private void assertEmployeePersisted(Employee input) {
|
||||
Employee employee = employeeRepository.getOne(input.getId());
|
||||
assertThat(employee).isNotNull();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user