원인
@SpringBootApplication
@EnableJpaAuditing
public class AloharoomBackendApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(AloharoomBackendApplication.class, args);
}
@PersistenceContext
EntityManager em;
@Override
public void run(String... args) throws Exception {
//Home 생성
BoardAddDto b1 = new BoardAddDto();
b1.setFlat(30); b1.setRoomCount(3); b1.setRent(25); b1.setHomeType("apartment"); b1.setStartDate(LocalDate.now());
b1.setAddress("서울특별시 성북구 삼선교로16길 116"); b1.setContents("방 게시물1 내용입니다.");
Home h1 = new Home(b1);
em.persist(h1);
...
실행 시 초기 데이터를 삽입하기 위해 위와 같이 코드를 작성했으나
현재 스레드에 EntityManager가 없어서 persist 할 수 없다고 한다.
해결
"actual transaction available for current thread" 라고 쓰여있어서 내 작업에 트랜잭션이 선언되어 있나 확인해 보았는데 역시 선언이 안 되어있었다.
기본적으로 JPA는 Transaction 기반으로 작동하게 되어있다.
Transaction 단위에 따라 1차캐시영역에 있는 객체들이 db에 flush 되어 영속화되기 때문이다. 하지만 그러한 영속작업을 하는 persist() 메소드에 객체가 들어갔으나 가능한 transaction이 존재하지 않았기에 저런 에러를 낸 것이다.
참고
https://sas-study.tistory.com/348
'오류 해결' 카테고리의 다른 글
[Spring] ddl-auto: create 안 됨 (1) | 2023.10.24 |
---|---|
[JPA] More than one row with the given identifier was found: 1 (0) | 2023.03.11 |
[Mysql] Cannot delete or update a parent row (0) | 2023.02.28 |
Java Mysql 연동 시 오류 발생 : java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed (0) | 2023.02.13 |
java.lang.StackOverflowError: null - @Data (0) | 2023.01.11 |