我有以下情况:有公司和员工。每家公司都有一组员工。每位员工都可以为多家公司工作。所以我实现了以下关系:
Company.class:
@JoinTable(name = "company_employee", joinColumns = @JoinColumn(name = "company_id") , inverseJoinColumns = @JoinColumn(name = "employee_id") )
@ManyToMany(fetch = FetchType.LAZY)
private List<Employee> employees;
Employee.class:
@JoinTable(name = "company_employee", joinColumns = @JoinColumn(name = "employee_id") , inverseJoinColumns = @JoinColumn(name = "company_id") )
@ManyToMany(fetch = FetchType.LAZY)
private List<Company> companies;
显然,要为多家公司工作,每位员工应该为他或她工作的每家公司分配几个不重叠的时间表。
此外,每个组合公司 - 员工应该有一个计划列表,因为有时旧计划到期,新计划生效。
所以我也有Schedule.class
,它应该与child to parent @ManyToOne
和Company
建立Employee
关系,并且应该遵循以下方式:每个Schedule
,因此,List<Schedule>
应该只对应Company
和Employee
个实例的一个组合。
如何实现这种关系?
更新1
我只考虑为每个@OneToMany Schedule
和Company
添加Employee
关系,但我需要将Schedule
的实例都添加到Company
和Employee
每次Schedule
,这种方式看起来不对,现在对我来说也不是很明显如何取回它。
所以任何帮助都将不胜感激。
此帖子已更新,以显示我所拥有的真实场景,而不仅仅是通用的Entity1,Entity2,类的Entity3名称。
更新2
我接受了答案,但如果Schedule包含列表,我就无法使用它。
根据我的计划,List<Vacation>
应该包含Vacations
,以了解一年内List
和Days
Days
的集合,每个Schedule
显示特别是工作日,休息和结束这一天。那些schedule_id
对于每个Schedule
实例也是唯一的。
它应该是类似下面的内容,但显然现在我没有@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "schedule_id")
private List<Vacation> vacations;
@JoinTable(name = "schedule_week", joinColumns = @JoinColumn(name = "schedule_id") , inverseJoinColumns = @JoinColumn(name = "day_id") )
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Day> week;
,那么如何将这些列表连接到en-GB
?
eng
如何正确包含这些列表?
答案 0 :(得分:1)
我想建议以下解决方案。
embeddable
类,其中包含特定时间表的Company
和Employee
。
@Embeddable
public class ScheduleOwner implements Serializable{
@MapsId("id")
@ManyToOne(cascade = CascadeType.ALL)
Company c;
@MapsId("id")
@ManyToOne(cascade = CascadeType.ALL)
Employee e;
}
Schedule
类正在嵌入ScheduleOwner
个实例。
@Entity
public class Schedule {
@EmbeddedId
ScheduleOwner owner;
String description;
}
Company
和Employee
类(未对其进行任何更改)
@Entity
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@JoinTable(name = "company_employee", joinColumns = @JoinColumn(name = "company_id") , inverseJoinColumns = @JoinColumn(name = "employee_id") )
@ManyToMany(fetch = FetchType.LAZY)
private List<Employee> employees;
}
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@JoinTable(name = "company_employee", joinColumns = @JoinColumn(name = "employee_id") , inverseJoinColumns = @JoinColumn(name = "company_id") )
@ManyToMany(fetch = FetchType.LAZY)
private List<Company> companies;
}
更新1
以下是保存和获取结果的方法。
Employee e1 = new Employee();
Company c1 = new Company();
c1.employees.add(e1);
e1.companies.add(c1);
ScheduleOwner so = new ScheduleOwner();
so.c = c1;
so.e = e1;
Schedule s = new Schedule();
s.owner = so;
session.save(c1);
session.save(e1);
session.save(s);
// below query will fetch from schedule, where company id = 9
Schedule ss = (Schedule) session.createQuery("From Schedule sh where sh.owner.c.id = 9").uniqueResult();
更新2
@Entity
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@JoinTable(name = "company_employee", joinColumns = @JoinColumn(name = "company_id", referencedColumnName="id")
, inverseJoinColumns = @JoinColumn(name = "employee_id", referencedColumnName="id"))
@ManyToMany(fetch = FetchType.LAZY)
List<Employee> employees = new ArrayList<>();
String name;
}
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "employees")
List<Company> companies = new ArrayList<>();
String name;
}
@Entity
public class Schedule {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int schedule_id;
@ManyToOne
@JoinColumn(name = "company_id", insertable = false, updatable = false)
private Company company;
@ManyToOne
@JoinColumn(name = "employee_id", insertable = false, updatable = false)
private Employee employee;
String description;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "schedule")
List<Vacation> vacations;
}
@Entity
public class Vacation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int vacation_id;
@ManyToOne
@JoinColumn(name = "schedule_id" )
Schedule schedule;
@OneToMany(mappedBy = "vacation")
List<Day> days;
}
Day
实体与Vacation
直接相关。不要Schedule
。
@Entity
public class Day {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToOne
@JoinColumn(name = "vacation_id")
Vacation vacation;
}
希望这有帮助。