작성
·
467
0
@Transactional
@Repository
public class JPQLMemberRepository implements MemberRepository{
@PersistenceContext
private final EntityManager em;
@Autowired
public JPQLMemberRepository(EntityManager em) {
this.em = em;
}
public EntityTransaction getTx() {
return em.getTransaction();
}
public Member save(Member member) {
em.persist(member);
return member;
}
public void emClose() {
em.close();
}
@Override
public Optional<Member> findByid(Long id) {
return Optional.empty();
}
@Override
public Optional<Member> findByname(String name) {
List<Member> result = em.createQuery(
"SELECT m FROM Member m WHERE m.name = :name",
Member.class
).setParameter("name", name).getResultList();
System.out.println("--------------------------------");
for (Member i : result) {
System.out.println("member:" + i);
}
System.out.println("--------------------------------");
return result.stream().findAny();
}
}
[ SpringConfig ]
@Configuration
public class SpringConfig {
@Bean
public EntityManagerFactory getEnf() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("5xik");
return emf;
}
@Bean
public EntityManager getEm() {
return getEnf().createEntityManager();
}
@Bean
public MemberRepository memberRepository() {
return new JPQLMemberRepository(getEm());
}
}
[ TestCode ]
@SpringBootTest
@Transactional
class JPQLMemberRepositoryTest {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
MemberRepository repository = ac.getBean("memberRepository",JPQLMemberRepository.class);
EntityTransaction tx = repository.getTx();
@Test
@Commit
void insertData() {
tx.begin();
try{
Member member = new Member();
member.setName("sungjun");
member.setPhonenumber("010");
repository.save(member);
Optional<Member> result = repository.findByname(member.getName());
Assertions.assertThat(result).isEqualTo(member);
tx.commit();
} catch (Exception e){
tx.rollback();
} finally {
//repository.emClose();
}
//ac.close();
}
}
그리고 아직 close를 하는 부분은 구현을 안해놔서
주석처리를 해놨습니다 !
감사합니다.
답변 1
0
안녕하세요. 준준님, 공식 서포터즈 OMG입니다.
본 강의는 스프링부트 환경도 아니고, 별도의 테스트를 작성하지는 않기 때문에 해당 내용은 숙지하고 계시다고 가정하고 구현하였습니다.
applicatio.properties
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:mem:testdb
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.example.demo.domain;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
@Getter @Setter
public class Member {
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String name;
}
package com.example.demo.repository;
import com.example.demo.domain.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import java.util.List;
import java.util.Optional;
@Transactional
@Repository
public class JPQLMemberRepository implements MemberRepository{
@Autowired
private EntityManager em;
public EntityTransaction getTx() {
return em.getTransaction();
}
public Member save(Member member) {
em.persist(member);
return member;
}
public void emClose() {
em.close();
}
@Override
public Optional<Member> findById(Long id) {
return Optional.empty();
}
@Override
public Optional<Member> findByName(String name) {
List<Member> result = em.createQuery(
"SELECT m FROM Member m WHERE m.name = :name", Member.class)
.setParameter("name", name)
.getResultList();
return result.stream().findAny();
}
}
package com.example.demo.repository;
import com.example.demo.domain.Member;
import java.util.Optional;
public interface MemberRepository {
Optional<Member> findById(Long id);
Optional<Member> findByName(String name);
Member save(Member member);
}
package com.example.demo.repository;
import com.example.demo.domain.Member;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
@Transactional
class JPQLMemberRepositoryTest {
@Autowired
MemberRepository repository;
@Test
void insertData() {
Member member = new Member();
member.setName("sungjun");
repository.save(member);
Member result = repository.findByName(member.getName()).get();
assertThat(result).isEqualTo(member);
}
}
감사합니다.
오.. 감사합니다 !!!