如何使用 JPA 连接 spring 工具套件中具有不同列名的 2 个表?

How to join 2 tables with different column name in spring tool suite with JPA?

是否可以用不同的列名连接 2 table? 这是技能模型 class

@Entity
@Table(name = "x_skill")
public class skillModel {
    @Id
    @Column(name="id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @Column(name="biodata_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long biodataId;
    @Column(name="skill_name")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private String skillName;
    @Column(name="skill_level_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long skillLevelId;
    @Column(name="is_delete")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private boolean isDelete;
}

这是我的技能水平模型

@Entity
@Table(name = "x_skill_level")
public class SkillLevelModel {
    private static final String ALL = null;
    @Id
    @Column(name="id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @Column(name="created_by")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long createdBy;
    @Column(name="created_on")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Timestamp createdOn;
    @Column(name="modified_by")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long modifiedBy;
    @Column(name="modified_on")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Timestamp modifiedOn;
    @Column(name="deleted_by")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long deletedBy;
    @Column(name="deleted_on")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Timestamp deletedOn;
    @Column(name="is_delete")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private boolean isDelete;
    @Column(name="name")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private String name;
    @Column(name="description")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private String description;
}

我尝试使用加入技能模型:

@ManyToOne
@JoinColumn(name="skill_level_id", referencedColumnName="id", nullable=true, updatable=false, insertable=false)
private SkillModel skm;

但是我得到了java.lang.IllegalArgumentException: Can not set long field com.minipro207.model.SkillModel.deletedBy to null value.

顺便说一句,来自 x_skill_level 的主键在 x_skill 的外键中被命名为 skill_level_id。 谢谢 :)

您需要在第二个实体中添加一个 @OneToMany,如下所示:

@Entity
@Table(name = "x_skill_level")
public class SkillLevelModel {
   // ...
   @OneToMany(mappedBy="skm", fetch=FetchType.LAZY)
   private List<SkillModel> skills;
   // ...
}

@Entity
@Table(name = "x_skill")
public class SkillModel {
   // ...
   @ManyToOne
   @JoinColumn(name="skill_level_id", referencedColumnName="id", nullable=true, updatable=false, insertable=false)
   private SkillLevelModel skm;
   // ...
}