Skip to content

Commit fb210d0

Browse files
FrancoCorleoneecheyne
authored andcommitted
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
1 parent 2af8ce7 commit fb210d0

9 files changed

+239
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.dao.repositories;
2+
3+
public interface InsertRepository<T> {
4+
<S extends T> void insert(S entity);
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.dao.repositories;
2+
3+
import com.baeldung.domain.Person;
4+
5+
public interface PersonEntityManagerInsertRepository {
6+
void insert(Person person);
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.dao.repositories;
2+
3+
import com.baeldung.domain.Person;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface PersonEntityManagerRepository extends JpaRepository<Person, Long>, PersonEntityManagerInsertRepository {
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.dao.repositories;
2+
3+
import com.baeldung.domain.Person;
4+
5+
public interface PersonQueryInsertRepository {
6+
void insert(Person person);
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.dao.repositories;
2+
3+
import com.baeldung.domain.Person;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Modifying;
6+
import org.springframework.data.jpa.repository.Query;
7+
import org.springframework.data.repository.query.Param;
8+
import org.springframework.stereotype.Repository;
9+
10+
@Repository
11+
public interface PersonQueryRepository extends JpaRepository<Person, Long>, PersonQueryInsertRepository {
12+
13+
@Modifying
14+
@Query(value = "INSERT INTO person (id, first_name, last_name) VALUES (:id,:firstName,:lastName)", nativeQuery = true)
15+
void insertWithAnnotation(@Param("id") Long id, @Param("firstName") String firstName, @Param("lastName") String lastName);
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.dao.repositories.impl;
2+
3+
import com.baeldung.dao.repositories.PersonEntityManagerInsertRepository;
4+
import com.baeldung.domain.Person;
5+
import org.springframework.transaction.annotation.Transactional;
6+
7+
import javax.persistence.EntityManager;
8+
import javax.persistence.PersistenceContext;
9+
10+
@Transactional
11+
public class PersonEntityManagerInsertRepositoryImpl implements PersonEntityManagerInsertRepository {
12+
13+
@PersistenceContext
14+
private EntityManager entityManager;
15+
16+
@Override
17+
public void insert(Person person) {
18+
this.entityManager.persist(person);
19+
}
20+
}
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.dao.repositories.impl;
2+
3+
import com.baeldung.dao.repositories.PersonQueryInsertRepository;
4+
import com.baeldung.domain.Person;
5+
6+
import javax.persistence.EntityManager;
7+
import javax.persistence.PersistenceContext;
8+
import javax.transaction.Transactional;
9+
10+
@Transactional
11+
public class PersonQueryInsertRepositoryImpl implements PersonQueryInsertRepository {
12+
13+
@PersistenceContext
14+
private EntityManager entityManager;
15+
16+
@Override
17+
public void insert(Person person) {
18+
entityManager.createNativeQuery("INSERT INTO person (id,first_name, last_name) VALUES (?,?,?)")
19+
.setParameter(1, person.getId())
20+
.setParameter(2, person.getFirstName())
21+
.setParameter(3, person.getLastName())
22+
.executeUpdate();
23+
}
24+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.domain;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
6+
@Entity
7+
public class Person {
8+
9+
@Id
10+
private Long id;
11+
private String firstName;
12+
private String lastName;
13+
14+
public Person() {
15+
}
16+
17+
public Person(Long id, String firstName, String lastName) {
18+
this.id = id;
19+
this.firstName = firstName;
20+
this.lastName = lastName;
21+
}
22+
23+
public Long getId() {
24+
return id;
25+
}
26+
27+
public void setId(Long id) {
28+
this.id = id;
29+
}
30+
31+
public String getFirstName() {
32+
return firstName;
33+
}
34+
35+
public void setFirstName(String firstName) {
36+
this.firstName = firstName;
37+
}
38+
39+
public String getLastName() {
40+
return lastName;
41+
}
42+
43+
public void setLastName(String lastName) {
44+
this.lastName = lastName;
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.baeldung.dao.repositories;
2+
3+
import com.baeldung.domain.Person;
4+
import org.junit.Test;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
9+
import org.springframework.dao.DataIntegrityViolationException;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
12+
import java.util.Optional;
13+
14+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
15+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
16+
17+
@RunWith(SpringRunner.class)
18+
@DataJpaTest
19+
public class PersonInsertRepositoryIntegrationTest {
20+
21+
private static final Long ID = 1L;
22+
private static final String FIRST_NAME = "firstname";
23+
private static final String LAST_NAME = "lastname";
24+
private static final Person PERSON = new Person(ID, FIRST_NAME, LAST_NAME);
25+
26+
@Autowired
27+
private PersonQueryRepository personQueryRepository;
28+
29+
@Autowired
30+
private PersonEntityManagerRepository personEntityManagerRepository;
31+
32+
@BeforeEach
33+
public void clearDB() {
34+
personQueryRepository.deleteAll();
35+
}
36+
37+
@Test
38+
public void givenPersonEntity_whenInsertWithNativeQuery_ThenPersonIsPersisted() {
39+
insertPerson();
40+
41+
assertPersonPersisted();
42+
}
43+
44+
@Test
45+
public void givenPersonEntity_whenInsertedTwiceWithNativeQuery_thenDataIntegrityViolationExceptionIsThrown() {
46+
assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy(() -> {
47+
insertPerson();
48+
insertPerson();
49+
});
50+
}
51+
52+
@Test
53+
public void givenPersonEntity_whenInsertWithQueryAnnotation_thenPersonIsPersisted() {
54+
insertPersonWithQueryAnnotation();
55+
56+
assertPersonPersisted();
57+
}
58+
59+
@Test
60+
public void givenPersonEntity_whenInsertedTwiceWithQueryAnnotation_thenDataIntegrityViolationExceptionIsThrown() {
61+
assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy(() -> {
62+
insertPersonWithQueryAnnotation();
63+
insertPersonWithQueryAnnotation();
64+
});
65+
}
66+
67+
@Test
68+
public void givenPersonEntity_whenInsertWithEntityManager_thenPersonIsPersisted() {
69+
insertPersonWithEntityManager();
70+
71+
assertPersonPersisted();
72+
}
73+
74+
@Test
75+
public void givenPersonEntity_whenInsertedTwiceWithEntityManager_thenDataIntegrityViolationExceptionIsThrown() {
76+
assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy(() -> {
77+
insertPersonWithEntityManager();
78+
insertPersonWithEntityManager();
79+
});
80+
}
81+
82+
private void insertPerson() {
83+
personQueryRepository.insert(PERSON);
84+
}
85+
86+
private void insertPersonWithQueryAnnotation() {
87+
personQueryRepository.insertWithAnnotation(ID, FIRST_NAME, LAST_NAME);
88+
}
89+
90+
private void insertPersonWithEntityManager() {
91+
personEntityManagerRepository.insert(new Person(ID, FIRST_NAME, LAST_NAME));
92+
}
93+
94+
private void assertPersonPersisted() {
95+
Optional<Person> personOptional = personQueryRepository.findById(PERSON.getId());
96+
97+
assertThat(personOptional.isPresent()).isTrue();
98+
assertThat(personOptional.get().getId()).isEqualTo(PERSON.getId());
99+
assertThat(personOptional.get().getFirstName()).isEqualTo(PERSON.getFirstName());
100+
assertThat(personOptional.get().getLastName()).isEqualTo(PERSON.getLastName());
101+
}
102+
}

0 commit comments

Comments
 (0)