用jparepositoryspringboot实现两个类之间的javaMap

2skhul33  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(304)

我有一个班级服务和一个班级雇员 Service.java ```
import lombok.Data;
import javax.persistence.*;

@Data
@Table(name = "services")
@Entity
public class ServiceResource {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String date;
private String longitude;
private String latitude;
@ManyToOne
@JoinColumn(name="employee_id")
private EmployeeResource employee;

}
`Employee.java`
import lombok.Data;
import javax.persistence.*;

@Data
@Table(name = "employees")
@Entity
public class EmployeeResource {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String longitude;
private String latitude;

}

现在我想构建一个应用程序,用jpa完成服务和雇员之间的Map。这可能吗?怎么可能?
mwngjboj

mwngjboj1#

将此添加到employeeresource实体:

@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL)
private List<ServiceResource> serviceList;

在serviceresource中,修改join列如下:

@ManyToOne
@JoinColumn(name="employee_id", referencedColumnName = "id")
private EmployeeResource employee;

相关问题