JPA 로 개발을 처음 해보는데 개발하는 시간보다 검색하는 시간이 더 많이 드는 것 같다. 

검색을 한참하다가 JPA에서 insert into select 를 지원하지 않는다는 글을 보고 너무 허망했다.
더 알아보기를 포기하고 그냥 쿼리를 작성해서 처리했다.

    # Controller 
    public String insertIntoSelect()throws Exception{
       // 로그
       service.insertIntoSelect(idx);
    }
    
    #Service
    public class Service {
       @Autowired
        private Repository repository;

        public void insertIntoSelect(Long idx) throws Exception{
            repository.insertIntoSelect(idx);
        }
    }
    
    #Repository
    @Repository
    public interface Repository extends JpaRepository<vo, Long> {
        @Transactional
        @Modifying
        @Query(value = "insert into table_nm1 ([컬럼명]) " +
                "select [컬럼명] from table_nm2 where table_nm2.idx = :idx", nativeQuery = true)
        void insertIntoSelect(@Param("idx") Long idx);
    }

 

'개발자 > JPA' 카테고리의 다른 글

[JPA/QueryDsl] select문에서 subQuery 사용하기  (0) 2021.08.12

+ Recent posts