Programing/JPA

JPA Entity 내 Subquery 로 동작하는 속성 추가 방법 @Formula

리커니 2024. 5. 8.
반응형

JPA Entity 에서 table 에는 존재하지 않지만, 특정 속성을 추가하여 관리해야되는 경우가 있습니다.

이렇 때 사용하는 것이 @Formula 이고, subqyery로 동작하게 됩니다.

 

예를들어 아래와 같은 테이블이 있습니다.

 

여기서 진행중인 프로젝트를 조회를 한다면

프로젝트 시작일자 <= 현재일자 <= 프로젝트 종료일자

 

진행중이지 않은 프로젝트를 조회한다면

현재일자 < 프로젝트 시작일자 OR 현재일자 > 프로젝트 종료일자

 

의 조건을 설정해야 합니다.

 

진행중 이라는 컬럼이 있으면 단순히 Y, N 으로 조회 할 수 있을 것 같습니다.

이럴 때 @Formula 를 사용하여 처리 할 수 있습니다.

 

@Getter
@Setter
@Entity(name = "project")
public class Project extends BaseEntity {

    @Id
    @GenericGenerator(name = "ProjectIdGenerator", type = IdGeneratorUtil.class
            , parameters = @org.hibernate.annotations.Parameter(name = GENERATOR_PARAM_KEY, value = "project"))
    @GeneratedValue(generator = "ProjectIdGenerator")
    @Column(name = "project_id")
    private String projectId;

    @Column(name = "project_name")
    private String projectName;

    @Column(name = "project_start_date")
    @Temporal(TemporalType.DATE)
    private LocalDate projectStartDate;

    @Column(name = "project_end_date")
    @Temporal(TemporalType.DATE)
    private LocalDate projectEndDate;

    @Formula("(select case when project_start_date >= now() or project_end_date  <= now() then 'Y' else 'N' end from project t1 where t1.project_id = project_id)")
    @Enumerated(EnumType.STRING)
    private UseYn isProceeding;
}

 

위와 같이 @Formula() 내에 String 으로 query를 작성하면 조회 시 subquery로 동작을 하게 됩니다.

로그를 보도록 하죠.

select
        p1_0.project_id,
        p1_0.create_dt,
        (select
            case 
                when p1_0.project_start_date >= now() 
                or p1_0.project_end_date  <= now() 
                    then 'Y' 
                else 'N' 
            end 
        from
            project t1 
        where
            t1.project_id = p1_0.project_id),
        p1_0.update_dt,
        p1_0.project_end_date,
        p1_0.project_name,
        p1_0.project_start_date 
    from
        project p1_0 
    where
        lower(p1_0.project_name) like ? escape '' 
    order by
        p1_0.create_dt desc nulls last

 

@Formula 로 설정한 필드는 Criteriabuilder 나 BooleanBuilder 의 조건으로도 활용이 가능합니다.

 

앞에서 말했다시피 @Formula 는 subquery로 동작하기 때문에 성능상 이슈를 고려하여 사용해야 합니다.

반응형

댓글

💲 추천 글