javax.persistence@column两个不同表的两列Map

2nbm6dog  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(323)

我尝试从3个表Map一个实体,使用@entity、@table、@secondarytables和@column注解,如下例所示。
这些表格是:1。员工(主要)2。员工详细信息3。工作描述

@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”
表“employee”中的“id”列和表“job\u description”中的“job\u code”列
此实体与以下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”Map列?这可能吗?

dkqlctbz

dkqlctbz1#

不,这是不可能的,因为jpa正在使用Map列进行读写。所以列不能连接。
但是您可以在类中编写一个简单的方法来连接字段。
另一种可能是使用sql语句的视图,但由于该视图包含多个表的字段,因此只能进行读写操作。

相关问题