일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- jquery
- guvnor
- ibatis
- custom filter
- maven
- zabbix
- JPA
- spring transaction
- jstl
- @SqlResultSetMapping
- COC
- JBoss Seam
- SVN
- Hudson
- bootstrap jquery datepicker
- Drools Fusion
- jquery serialize
- java tip
- Spring
- jenkins
- spring jpa
- CEP
- querydsl
- GEventEvaluator
- spring security
- gwt
- drools
- MySQL
- gwt-ext
- rember me
- Today
- Total
봉 블로그
Spring JPA 에서 POJO 사용하기 본문
Spring JPA 를 사용하면서 특정 Entity 와는 다른 데이타(field 조합)를 쿼리할경우가 있다.
또는
Entity 의 특정 field 만 쿼리할경우는 아래와 같이 하면 된다.
----------- 아래는 Entity class 를 사용하는경우 ---------
@Repository
public interface BoardRepository extends JpaRepository<Board, Integer>, JpaSpecificationExecutor<Board> {
@Query(value = "SELECT new Board(b.id, b.subject, b.userId, b.createDt) FROM Board b")
Page<Board> findList(Pageable pageable);
}
----------- 아래는 POJO class 를 사용하는경우 ---------
@Query(value = "SELECT new com.bong.bbs.BoardRow(b.id, b.subject, b.userId, b.createDt) FROM Board b")
Page<BoardRow> findList(Pageable pageable);
-------- 아래 생성자가 있어야 함. BoardRow 도 마찬가지 ---
public Board(Integer id, String subject, Long userId, Date createDt) {
super();
this.id = id;
this.subject = subject;
this.userId = userId;
this.createDt = createDt;
}
@SqlResultSetMapping 를 사용할수도 있지만 복잡하다.
native query 결과를 pojo 로 리턴받고 싶다면 @SqlResultSetMapping 를 사용하는것이 좋겠다.