BAEL-5263 : Access Entity Manager with Spring Boot and Spring Data (#11680)

* feat: entity manager with spring data

* fix typo

* fix: article and code review
This commit is contained in:
lucaCambi77 2022-01-23 04:36:25 +01:00 committed by GitHub
parent 7dde535340
commit 4d159f7808
6 changed files with 139 additions and 0 deletions

View File

@ -0,0 +1,12 @@
package com.baeldung.spring.data.persistence.customrepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CustomRepositoryApplication {
public static void main(String[] args) {
SpringApplication.run(CustomRepositoryApplication.class, args);
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.spring.data.persistence.customrepository.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Objects;
@Entity
public class User {
@Id
@GeneratedValue
private Long id;
private String name;
private String email;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
User user = (User) o;
return Objects.equals(id, user.id) && Objects.equals(name, user.name) && Objects.equals(email, user.email);
}
@Override
public int hashCode() {
return Objects.hash(id, name, email);
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.spring.data.persistence.customrepository.repository;
import com.baeldung.spring.data.persistence.customrepository.model.User;
public interface CustomUserRepository {
User customFindMethod(Long id);
}

View File

@ -0,0 +1,26 @@
package com.baeldung.spring.data.persistence.customrepository.repository;
import com.baeldung.spring.data.persistence.customrepository.model.User;
import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Objects;
public class CustomUserRepositoryImpl implements CustomUserRepository {
@PersistenceContext
private EntityManager entityManager;
@Override
public User customFindMethod(Long id) {
return (User) entityManager.createQuery("FROM User u WHERE u.id = :id")
.setParameter("id", id)
.getSingleResult();
}
@PostConstruct
public void postConstruct() {
Objects.requireNonNull(entityManager);
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.spring.data.persistence.customrepository.repository;
import com.baeldung.spring.data.persistence.customrepository.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long>, CustomUserRepository {
}

View File

@ -0,0 +1,27 @@
package com.baeldung.spring.data.persistence.customrepository;
import com.baeldung.spring.data.persistence.customrepository.model.User;
import com.baeldung.spring.data.persistence.customrepository.repository.UserRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest(classes = CustomRepositoryApplication.class)
class CustomRepositoryUnitTest {
@Autowired
private UserRepository userRepository;
@Test
void givenCustomRepository_whenInvokeCustomFindMethod_thenEntityIsFound() {
User user = new User();
user.setEmail("foo@gmail.com");
user.setName("userName");
User persistedUser = userRepository.save(user);
assertEquals(persistedUser, userRepository.customFindMethod(user.getId()));
}
}