[BAEL-2566] Add example of Insert with Modifying Native Query (#6164)
* [BAEL-2566] Add example of Insert with Modifying Native Query * [BAEL-2557] Fix MR conflicts * [BAEL-2557] Cleanup tests
This commit is contained in:
parent
96fbef6a14
commit
243c671edb
@ -67,10 +67,14 @@ public interface UserRepository extends JpaRepository<User, Integer> , UserRepos
|
|||||||
@Query(value = "UPDATE Users u SET u.status = ? WHERE u.name = ?", nativeQuery = true)
|
@Query(value = "UPDATE Users u SET u.status = ? WHERE u.name = ?", nativeQuery = true)
|
||||||
int updateUserSetStatusForNameNative(Integer status, String name);
|
int updateUserSetStatusForNameNative(Integer status, String name);
|
||||||
|
|
||||||
|
@Query(value = "INSERT INTO Users (name, age, email, status) VALUES (:name, :age, :email, :status)", nativeQuery = true)
|
||||||
|
@Modifying
|
||||||
|
void insertUser(@Param("name") String name, @Param("age") Integer age, @Param("status") Integer status, @Param("email") String email);
|
||||||
|
|
||||||
@Modifying
|
@Modifying
|
||||||
@Query(value = "UPDATE Users u SET status = ? WHERE u.name = ?", nativeQuery = true)
|
@Query(value = "UPDATE Users u SET status = ? WHERE u.name = ?", nativeQuery = true)
|
||||||
int updateUserSetStatusForNameNativePostgres(Integer status, String name);
|
int updateUserSetStatusForNameNativePostgres(Integer status, String name);
|
||||||
|
|
||||||
@Query(value = "SELECT u FROM User u WHERE u.name IN :names")
|
@Query(value = "SELECT u FROM User u WHERE u.name IN :names")
|
||||||
List<User> findUserByNameList(@Param("names") Collection<String> names);
|
List<User> findUserByNameList(@Param("names") Collection<String> names);
|
||||||
}
|
}
|
||||||
|
@ -12,11 +12,7 @@ import org.springframework.data.jpa.domain.JpaSort;
|
|||||||
import org.springframework.data.mapping.PropertyReferenceException;
|
import org.springframework.data.mapping.PropertyReferenceException;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
@ -29,10 +25,10 @@ class UserRepositoryCommon {
|
|||||||
final String USER_EMAIL4 = "email4@example.com";
|
final String USER_EMAIL4 = "email4@example.com";
|
||||||
final Integer INACTIVE_STATUS = 0;
|
final Integer INACTIVE_STATUS = 0;
|
||||||
final Integer ACTIVE_STATUS = 1;
|
final Integer ACTIVE_STATUS = 1;
|
||||||
private final String USER_EMAIL5 = "email5@example.com";
|
final String USER_EMAIL5 = "email5@example.com";
|
||||||
private final String USER_EMAIL6 = "email6@example.com";
|
final String USER_EMAIL6 = "email6@example.com";
|
||||||
private final String USER_NAME_ADAM = "Adam";
|
final String USER_NAME_ADAM = "Adam";
|
||||||
private final String USER_NAME_PETER = "Peter";
|
final String USER_NAME_PETER = "Peter";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
protected UserRepository userRepository;
|
protected UserRepository userRepository;
|
||||||
@ -384,6 +380,22 @@ class UserRepositoryCommon {
|
|||||||
|
|
||||||
assertThat(usersWithNames.size()).isEqualTo(2);
|
assertThat(usersWithNames.size()).isEqualTo(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Transactional
|
||||||
|
public void whenInsertedWithQuery_ThenUserIsPersisted() {
|
||||||
|
userRepository.insertUser(USER_NAME_ADAM, 1, ACTIVE_STATUS, USER_EMAIL);
|
||||||
|
userRepository.insertUser(USER_NAME_PETER, 1, ACTIVE_STATUS, USER_EMAIL2);
|
||||||
|
|
||||||
|
User userAdam = userRepository.findUserByNameLike(USER_NAME_ADAM);
|
||||||
|
User userPeter = userRepository.findUserByNameLike(USER_NAME_PETER);
|
||||||
|
|
||||||
|
assertThat(userAdam).isNotNull();
|
||||||
|
assertThat(userAdam.getEmail()).isEqualTo(USER_EMAIL);
|
||||||
|
assertThat(userPeter).isNotNull();
|
||||||
|
assertThat(userPeter.getEmail()).isEqualTo(USER_EMAIL2);
|
||||||
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void cleanUp() {
|
public void cleanUp() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user