2017-11-20 12 views
2

우리는 마이그레이션 프로젝트를 진행하고 있으며 HBM 파일에서 주석으로 마이그레이션 중입니다.@ManyToOne 연관을 SQL 쿼리 결과에 매핑하는 방법

기본적으로 동일한 클래스의 인스턴스 인 속성에 값을 설정하려고하면 반복되는 열 매핑 문제가 발생합니다.

public class Salary{ 

    // All the below are coming from lookup table like empLookUp, MonthLookup, 
    // YearLookup, CurrencyLookUp and they are joined using their primary key 

    private int empId; 
    private int month; 
    private int year; 
    private String currency; 

    // Issue here: previousMonthSalary actually needs to be populated when the 
    // Salary is loaded, but for previous month. How do I achieve this. 

    private Salary previousMonthSalary; 

} 

previousMonthSalary을 매핑하는 방법은 무엇입니까?

+1

주석을 사용하고 ('Public'과 같은 유효하지 않은 항목없이) 실제 코드를 게시하고 예외의 전체 스택 추적을 게시하십시오. –

+0

문제가 무엇인지 설명하거나 보여주십시오. 예외 또는 잘못된 결과, .. –

답변

1

this article에서 설명한 것처럼 대부분 @JoinFormula annotation을 사용해야합니다.

@ManyToOne(fetch = FetchType.LAZY) 
@JoinFormula("(" + 
    "SELECT s.id " + 
    "FROM salary s " + 
    "WHERE s.empId = empId " + 
    "AND CASE WHEN month = 1 THEN s.year + 1 = year AND s.month = 12 ELSE s.year = year AND s.month - 1 = month END " 
")") 
private Salary previousMonthSalary; 
0

previousMonthSalary 변수에 lazy fetch를 사용하면 문제가 해결되기를 바랍니다.

@ManyToOne(fetch=fetchType.Lazy) 
private Salary previousMonthSalary;