gpt4 book ai didi

java - 如何将 @ManyToOne 关联映射到 SQL 查询的结果

转载 作者:行者123 更新时间:2023-12-04 13:22:49 25 4
gpt4 key购买 nike

我们正在进行一个迁移项目,我们正在从 HBM 文件迁移到注释。

当我尝试为属性设置值时,我们面临重复的列映射问题,这基本上是同一类的实例。

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

最佳答案

很可能,您需要使用 @JoinFormula 注释:

@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;

关于java - 如何将 @ManyToOne 关联映射到 SQL 查询的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47399673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com