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;
|
package com.baeldung.dao.repositories.impl;
|
||||||
|
|
||||||
import com.baeldung.dao.repositories.PersonQueryInsertRepository;
|
|
||||||
import com.baeldung.domain.Person;
|
import com.baeldung.domain.Person;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.PersistenceContext;
|
import javax.persistence.PersistenceContext;
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
|
|
||||||
@Transactional
|
@Repository
|
||||||
public class PersonQueryInsertRepositoryImpl implements PersonQueryInsertRepository {
|
public class PersonInsertRepository {
|
||||||
|
|
||||||
@PersistenceContext
|
@PersistenceContext
|
||||||
private EntityManager entityManager;
|
private EntityManager entityManager;
|
||||||
|
|
||||||
@Override
|
@Transactional
|
||||||
public void insert(Person person) {
|
public void insertWithQuery(Person person) {
|
||||||
entityManager.createNativeQuery("INSERT INTO person (id,first_name, last_name) VALUES (?,?,?)")
|
entityManager.createNativeQuery("INSERT INTO person (id, first_name, last_name) VALUES (?,?,?)")
|
||||||
.setParameter(1, person.getId())
|
.setParameter(1, person.getId())
|
||||||
.setParameter(2, person.getFirstName())
|
.setParameter(2, person.getFirstName())
|
||||||
.setParameter(3, person.getLastName())
|
.setParameter(3, person.getLastName())
|
||||||
.executeUpdate();
|
.executeUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void insertWithEntityManager(Person person) {
|
||||||
|
this.entityManager.persist(person);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,21 +1,24 @@
|
||||||
package com.baeldung.dao.repositories;
|
package com.baeldung.dao.repositories;
|
||||||
|
|
||||||
|
import com.baeldung.dao.repositories.impl.PersonInsertRepository;
|
||||||
import com.baeldung.domain.Person;
|
import com.baeldung.domain.Person;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
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 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.Assertions.assertThatExceptionOfType;
|
||||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@DataJpaTest
|
@DataJpaTest
|
||||||
|
@Import(PersonInsertRepository.class)
|
||||||
public class PersonInsertRepositoryIntegrationTest {
|
public class PersonInsertRepositoryIntegrationTest {
|
||||||
|
|
||||||
private static final Long ID = 1L;
|
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);
|
private static final Person PERSON = new Person(ID, FIRST_NAME, LAST_NAME);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private PersonQueryRepository personQueryRepository;
|
private PersonInsertRepository personInsertRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private PersonEntityManagerRepository personEntityManagerRepository;
|
private EntityManager entityManager;
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
public void clearDB() {
|
|
||||||
personQueryRepository.deleteAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPersonEntity_whenInsertWithNativeQuery_ThenPersonIsPersisted() {
|
public void givenPersonEntity_whenInsertWithNativeQuery_ThenPersonIsPersisted() {
|
||||||
insertPerson();
|
insertWithQuery();
|
||||||
|
|
||||||
assertPersonPersisted();
|
assertPersonPersisted();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPersonEntity_whenInsertedTwiceWithNativeQuery_thenDataIntegrityViolationExceptionIsThrown() {
|
public void givenPersonEntity_whenInsertedTwiceWithNativeQuery_thenPersistenceExceptionExceptionIsThrown() {
|
||||||
assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy(() -> {
|
assertThatExceptionOfType(PersistenceException.class).isThrownBy(() -> {
|
||||||
insertPerson();
|
insertWithQuery();
|
||||||
insertPerson();
|
insertWithQuery();
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenPersonEntity_whenInsertWithQueryAnnotation_thenPersonIsPersisted() {
|
|
||||||
insertPersonWithQueryAnnotation();
|
|
||||||
|
|
||||||
assertPersonPersisted();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenPersonEntity_whenInsertedTwiceWithQueryAnnotation_thenDataIntegrityViolationExceptionIsThrown() {
|
|
||||||
assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy(() -> {
|
|
||||||
insertPersonWithQueryAnnotation();
|
|
||||||
insertPersonWithQueryAnnotation();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,31 +55,27 @@ public class PersonInsertRepositoryIntegrationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPersonEntity_whenInsertedTwiceWithEntityManager_thenDataIntegrityViolationExceptionIsThrown() {
|
public void givenPersonEntity_whenInsertedTwiceWithEntityManager_thenEntityExistsExceptionIsThrown() {
|
||||||
assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy(() -> {
|
assertThatExceptionOfType(EntityExistsException.class).isThrownBy(() -> {
|
||||||
insertPersonWithEntityManager();
|
insertPersonWithEntityManager();
|
||||||
insertPersonWithEntityManager();
|
insertPersonWithEntityManager();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void insertPerson() {
|
private void insertWithQuery() {
|
||||||
personQueryRepository.insert(PERSON);
|
personInsertRepository.insertWithQuery(PERSON);
|
||||||
}
|
|
||||||
|
|
||||||
private void insertPersonWithQueryAnnotation() {
|
|
||||||
personQueryRepository.insertWithAnnotation(ID, FIRST_NAME, LAST_NAME);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void insertPersonWithEntityManager() {
|
private void insertPersonWithEntityManager() {
|
||||||
personEntityManagerRepository.insert(new Person(ID, FIRST_NAME, LAST_NAME));
|
personInsertRepository.insertWithEntityManager(new Person(ID, FIRST_NAME, LAST_NAME));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertPersonPersisted() {
|
private void assertPersonPersisted() {
|
||||||
Optional<Person> personOptional = personQueryRepository.findById(PERSON.getId());
|
Person person = entityManager.find(Person.class, ID);
|
||||||
|
|
||||||
assertThat(personOptional.isPresent()).isTrue();
|
assertThat(person).isNotNull();
|
||||||
assertThat(personOptional.get().getId()).isEqualTo(PERSON.getId());
|
assertThat(person.getId()).isEqualTo(PERSON.getId());
|
||||||
assertThat(personOptional.get().getFirstName()).isEqualTo(PERSON.getFirstName());
|
assertThat(person.getFirstName()).isEqualTo(PERSON.getFirstName());
|
||||||
assertThat(personOptional.get().getLastName()).isEqualTo(PERSON.getLastName());
|
assertThat(person.getLastName()).isEqualTo(PERSON.getLastName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue