moved jpa insert examples from spring-data-jpa to spring-data-jpa-3
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
### Relevant Articles:
|
||||
- [Limiting Query Results with JPA and Spring Data JPA](https://www.baeldung.com/jpa-limit-query-results)
|
||||
- [Sorting Query Results with Spring Data](https://www.baeldung.com/spring-data-sorting)
|
||||
- [Sorting Query Results with Spring Data](https://www.baeldung.com/spring-data-sorting)
|
||||
- [INSERT Statement in JPA](https://www.baeldung.com/jpa-insert)
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.boot.daos.impl;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.boot.domain.Person;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
@Repository
|
||||
public class PersonInsertRepository {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.boot.domain;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(Long id, String firstName, String lastName) {
|
||||
this.id = id;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.baeldung.boot.daos;
|
||||
|
||||
import com.baeldung.boot.daos.impl.PersonInsertRepository;
|
||||
import com.baeldung.boot.domain.Person;
|
||||
|
||||
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.context.annotation.Import;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
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;
|
||||
private static final String FIRST_NAME = "firstname";
|
||||
private static final String LAST_NAME = "lastname";
|
||||
private static final Person PERSON = new Person(ID, FIRST_NAME, LAST_NAME);
|
||||
|
||||
@Autowired
|
||||
private PersonInsertRepository personInsertRepository;
|
||||
|
||||
@Autowired
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Test
|
||||
public void givenPersonEntity_whenInsertWithNativeQuery_ThenPersonIsPersisted() {
|
||||
insertWithQuery();
|
||||
|
||||
assertPersonPersisted();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonEntity_whenInsertedTwiceWithNativeQuery_thenPersistenceExceptionExceptionIsThrown() {
|
||||
assertThatExceptionOfType(PersistenceException.class).isThrownBy(() -> {
|
||||
insertWithQuery();
|
||||
insertWithQuery();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonEntity_whenInsertWithEntityManager_thenPersonIsPersisted() {
|
||||
insertPersonWithEntityManager();
|
||||
|
||||
assertPersonPersisted();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonEntity_whenInsertedTwiceWithEntityManager_thenEntityExistsExceptionIsThrown() {
|
||||
assertThatExceptionOfType(EntityExistsException.class).isThrownBy(() -> {
|
||||
insertPersonWithEntityManager();
|
||||
insertPersonWithEntityManager();
|
||||
});
|
||||
}
|
||||
|
||||
private void insertWithQuery() {
|
||||
personInsertRepository.insertWithQuery(PERSON);
|
||||
}
|
||||
|
||||
private void insertPersonWithEntityManager() {
|
||||
personInsertRepository.insertWithEntityManager(new Person(ID, FIRST_NAME, LAST_NAME));
|
||||
}
|
||||
|
||||
private void assertPersonPersisted() {
|
||||
Person person = entityManager.find(Person.class, ID);
|
||||
|
||||
assertThat(person).isNotNull();
|
||||
assertThat(person.getId()).isEqualTo(PERSON.getId());
|
||||
assertThat(person.getFirstName()).isEqualTo(PERSON.getFirstName());
|
||||
assertThat(person.getLastName()).isEqualTo(PERSON.getLastName());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user