BAEL-2090 Insert with JPA
* Adding inserts for JPA objects repositories * Refactor tests * Small refactor * Small refactor * Remove redundant @Repository annotation * Refactor tests * Change to simple entity manager implementation * Refactor changes * Remove Spring repositories, Add simple implementation using EntityManager * Remove redundant Interface * Remove redundant files * Update persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/impl/PersonInsertRepository.java Co-Authored-By: FrancoCorleone <jedfra6@gmail.com>
This commit is contained in:
parent
a1b1e02e71
commit
2c801888e7
|
@ -1,5 +0,0 @@
|
|||
package com.baeldung.dao.repositories;
|
||||
|
||||
public interface InsertRepository<T> {
|
||||
<S extends T> void insert(S entity);
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
package com.baeldung.dao.repositories;
|
||||
|
||||
import com.baeldung.domain.Person;
|
||||
|
||||
public interface PersonEntityManagerInsertRepository {
|
||||
void insert(Person person);
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
package com.baeldung.dao.repositories;
|
||||
|
||||
import com.baeldung.domain.Person;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface PersonEntityManagerRepository extends JpaRepository<Person, Long>, PersonEntityManagerInsertRepository {
|
||||
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
package com.baeldung.dao.repositories;
|
||||
|
||||
import com.baeldung.domain.Person;
|
||||
|
||||
public interface PersonQueryInsertRepository {
|
||||
void insert(Person person);
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
package com.baeldung.dao.repositories;
|
||||
|
||||
import com.baeldung.domain.Person;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface PersonQueryRepository extends JpaRepository<Person, Long>, PersonQueryInsertRepository {
|
||||
|
||||
@Modifying
|
||||
@Query(value = "INSERT INTO person (id, first_name, last_name) VALUES (:id,:firstName,:lastName)", nativeQuery = true)
|
||||
void insertWithAnnotation(@Param("id") Long id, @Param("firstName") String firstName, @Param("lastName") String lastName);
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
package com.baeldung.dao.repositories.impl;
|
||||
|
||||
import com.baeldung.dao.repositories.PersonEntityManagerInsertRepository;
|
||||
import com.baeldung.domain.Person;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
@Transactional
|
||||
public class PersonEntityManagerInsertRepositoryImpl implements PersonEntityManagerInsertRepository {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Override
|
||||
public void insert(Person person) {
|
||||
this.entityManager.persist(person);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,24 +1,30 @@
|
|||
package com.baeldung.dao.repositories.impl;
|
||||
|
||||
import com.baeldung.dao.repositories.PersonQueryInsertRepository;
|
||||
import com.baeldung.domain.Person;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
@Transactional
|
||||
public class PersonQueryInsertRepositoryImpl implements PersonQueryInsertRepository {
|
||||
@Repository
|
||||
public class PersonInsertRepository {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Override
|
||||
public void insert(Person person) {
|
||||
entityManager.createNativeQuery("INSERT INTO person (id,first_name, last_name) VALUES (?,?,?)")
|
||||
@Transactional
|
||||
public void insertWithQuery(Person person) {
|
||||
entityManager.createNativeQuery("INSERT INTO person (id, first_name, last_name) VALUES (?,?,?)")
|
||||
.setParameter(1, person.getId())
|
||||
.setParameter(2, person.getFirstName())
|
||||
.setParameter(3, person.getLastName())
|
||||
.executeUpdate();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void insertWithEntityManager(Person person) {
|
||||
this.entityManager.persist(person);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,21 +1,24 @@
|
|||
package com.baeldung.dao.repositories;
|
||||
|
||||
import com.baeldung.dao.repositories.impl.PersonInsertRepository;
|
||||
import com.baeldung.domain.Person;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Optional;
|
||||
import javax.persistence.EntityExistsException;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DataJpaTest
|
||||
@Import(PersonInsertRepository.class)
|
||||
public class PersonInsertRepositoryIntegrationTest {
|
||||
|
||||
private static final Long ID = 1L;
|
||||
|
@ -24,43 +27,23 @@ public class PersonInsertRepositoryIntegrationTest {
|
|||
private static final Person PERSON = new Person(ID, FIRST_NAME, LAST_NAME);
|
||||
|
||||
@Autowired
|
||||
private PersonQueryRepository personQueryRepository;
|
||||
private PersonInsertRepository personInsertRepository;
|
||||
|
||||
@Autowired
|
||||
private PersonEntityManagerRepository personEntityManagerRepository;
|
||||
|
||||
@BeforeEach
|
||||
public void clearDB() {
|
||||
personQueryRepository.deleteAll();
|
||||
}
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Test
|
||||
public void givenPersonEntity_whenInsertWithNativeQuery_ThenPersonIsPersisted() {
|
||||
insertPerson();
|
||||
insertWithQuery();
|
||||
|
||||
assertPersonPersisted();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonEntity_whenInsertedTwiceWithNativeQuery_thenDataIntegrityViolationExceptionIsThrown() {
|
||||
assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy(() -> {
|
||||
insertPerson();
|
||||
insertPerson();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonEntity_whenInsertWithQueryAnnotation_thenPersonIsPersisted() {
|
||||
insertPersonWithQueryAnnotation();
|
||||
|
||||
assertPersonPersisted();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonEntity_whenInsertedTwiceWithQueryAnnotation_thenDataIntegrityViolationExceptionIsThrown() {
|
||||
assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy(() -> {
|
||||
insertPersonWithQueryAnnotation();
|
||||
insertPersonWithQueryAnnotation();
|
||||
public void givenPersonEntity_whenInsertedTwiceWithNativeQuery_thenPersistenceExceptionExceptionIsThrown() {
|
||||
assertThatExceptionOfType(PersistenceException.class).isThrownBy(() -> {
|
||||
insertWithQuery();
|
||||
insertWithQuery();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -72,31 +55,27 @@ public class PersonInsertRepositoryIntegrationTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonEntity_whenInsertedTwiceWithEntityManager_thenDataIntegrityViolationExceptionIsThrown() {
|
||||
assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy(() -> {
|
||||
public void givenPersonEntity_whenInsertedTwiceWithEntityManager_thenEntityExistsExceptionIsThrown() {
|
||||
assertThatExceptionOfType(EntityExistsException.class).isThrownBy(() -> {
|
||||
insertPersonWithEntityManager();
|
||||
insertPersonWithEntityManager();
|
||||
});
|
||||
}
|
||||
|
||||
private void insertPerson() {
|
||||
personQueryRepository.insert(PERSON);
|
||||
}
|
||||
|
||||
private void insertPersonWithQueryAnnotation() {
|
||||
personQueryRepository.insertWithAnnotation(ID, FIRST_NAME, LAST_NAME);
|
||||
private void insertWithQuery() {
|
||||
personInsertRepository.insertWithQuery(PERSON);
|
||||
}
|
||||
|
||||
private void insertPersonWithEntityManager() {
|
||||
personEntityManagerRepository.insert(new Person(ID, FIRST_NAME, LAST_NAME));
|
||||
personInsertRepository.insertWithEntityManager(new Person(ID, FIRST_NAME, LAST_NAME));
|
||||
}
|
||||
|
||||
private void assertPersonPersisted() {
|
||||
Optional<Person> personOptional = personQueryRepository.findById(PERSON.getId());
|
||||
Person person = entityManager.find(Person.class, ID);
|
||||
|
||||
assertThat(personOptional.isPresent()).isTrue();
|
||||
assertThat(personOptional.get().getId()).isEqualTo(PERSON.getId());
|
||||
assertThat(personOptional.get().getFirstName()).isEqualTo(PERSON.getFirstName());
|
||||
assertThat(personOptional.get().getLastName()).isEqualTo(PERSON.getLastName());
|
||||
assertThat(person).isNotNull();
|
||||
assertThat(person.getId()).isEqualTo(PERSON.getId());
|
||||
assertThat(person.getFirstName()).isEqualTo(PERSON.getFirstName());
|
||||
assertThat(person.getLastName()).isEqualTo(PERSON.getLastName());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue