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
This commit is contained in:
committed by
Emily Cheyne
parent
2af8ce7656
commit
fb210d0499
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
public interface InsertRepository<T> {
|
||||
<S extends T> void insert(S entity);
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import com.baeldung.domain.Person;
|
||||
|
||||
public interface PersonEntityManagerInsertRepository {
|
||||
void insert(Person person);
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
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 {
|
||||
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import com.baeldung.domain.Person;
|
||||
|
||||
public interface PersonQueryInsertRepository {
|
||||
void insert(Person person);
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
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);
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.dao.repositories.impl;
|
||||
|
||||
import com.baeldung.dao.repositories.PersonQueryInsertRepository;
|
||||
import com.baeldung.domain.Person;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
@Transactional
|
||||
public class PersonQueryInsertRepositoryImpl implements PersonQueryInsertRepository {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Override
|
||||
public void insert(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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user