javax.persistence @Column 映射两个不同的2列table

javax.persistence @Column mapping of 2 columns of two different table

我正在尝试使用@Entity、@Table、@SecondaryTables 和@Column 注释从 3 tables 映射一个实体,如下例所示.

Table 是: 1. 员工(初级) 2. employee_detail 3.job_description

@Entity    
@Table(name="EMPLOYEE")    
@SecondaryTables({    
    @SecondaryTable(name="EMPLOYEE_DETAIL",    
        pkJoinColumns = @PrimaryKeyJoinColumn(name="EMPLOYEE_ID")),    
    @SecondaryTable(name="JOB_DESCRIPTION",    
        pkJoinColumns = @PrimaryKeyJoinColumn(name="JD_ID"))    
})
public class Employee {

    @Id
    @Column(name = "ID", table= "EMPLOYEE")
    private int id;

    @Column(name = "FIRST_NAME", table = "EMPLOYEE")
    private String firstname;

    @Column(name = "LAST_NAME", table = "EMPLOYEE")
    private String lastname;

    @Column(name = "BANK_ACCOUNT_NO", table = "EMPLOYEE_DETAIL") 
    private String bankacctnumber;

    @Column(name = "JOB_SUMMARY", table = "JOB_DESCRIPTION") 
    private String jobsummary;

    @Column(name = "     ???      ", table = "     ??        ")
    private String uniqueid;

    //getters and setters for above fields
    ...

}

我的问题是,如果我想通过连接

来创建字段 "uniqueid"

table 中的 "ID" 列 "EMPLOYEE" AND table 中的 "JOB_CODE" 列 "JOB_DESCRIPTION"

此实体对应于以下 sql 查询字符串(为清楚起见,我使用了字符串生成器):-

StringBuilder sql = new StringBuilder();

sql.append("SELECT");
sql.append(" e.FIRST_NAME AS firstname,");
sql.append(" e.LAST_NAME AS lastname,");
sql.append(" d.BANK_ACCT_NUMBER AS bankacctnumber,");
sql.append(" j.JOB_SUMMARY AS jobsummary,");
sql.append(" CONCAT(e.ID,SUBSTR(j.JOB_CODE,3,8)) AS uniqueid"); 
sql.append(" FROM employee e");
sql.append(" LEFT JOIN employee_detail d ON d.EMPLOYEE_ID = e.ID");
sql.append(" LEFT JOIN job_description j ON j.JD_ID = e.JD_ID ");
sql.append(" WHERE e.ID = 1 ");

究竟应该如何为字段"uniqueid"完成列映射?这可能吗?

不,这是不可能的,因为 JPA 正在使用映射列进行读写。所以不能连接一列。

但是您可以在 class 中编写一个连接字段的简单方法。

另一种可能是使用您的 SQL 语句的视图,但您现在只能读取和写入,因为该视图包含多个 table.

的字段