Jpa-Jpql查询以在OneToOneMapping中从父实体选择所有字段,从子实体选择特定字段

时间:2019-12-21 18:42:06

标签: spring-boot join spring-data-jpa spring-data jpql

我在员工和学生实体之间有一个OneToOne RelationShip,我想从员工实体中获取所有字段,并从笔记本电脑实体中获取姓名和型号。

员工实体

public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "employee_name")
private String name;

@Column(name = "date_of_birth")
private String dob;

@Column(name = "gender")
private char gender;

@Column(name = "skills")
private String[] skills;

@OneToOne(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
@JoinColumn(name = "laptop_id")
private Laptop laptop;

 //getter setter

笔记本电脑实体

@Entity
public class Laptop {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String name;

private String ram;

private String modelNumber;

private String processor;

//getter Setter

1 个答案:

答案 0 :(得分:0)

select e.id, e.name, e.dob, e.gender, e.skills, 
       e.laptop.name, e.laptop.modelNumber 
from Employee e

请先阅读文档:Sample Working Code Demo