gpt4 book ai didi

java - Hibernate 两父一子映射

转载 作者:行者123 更新时间:2023-11-30 10:52:41 25 4
gpt4 key购买 nike

我有以下场景:有公司和员工。每个公司都有一组员工。每个员工可以为多家公司工作。所以我实现了以下关系:

公司类:

@JoinTable(name = "company_employee", joinColumns = @JoinColumn(name = "company_id") , inverseJoinColumns = @JoinColumn(name = "employee_id") )
@ManyToMany(fetch = FetchType.LAZY)
private List<Employee> employees;

员工类:

@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 @ManyToOneCompany 的关系和 Employee ,并且应该按照以下方式工作:每个 Schedule ,因此,List<Schedule>应该恰好对应于 Company 的一个组合和 Employee实例。如何实现这种关系?

更新 1

我只想添加 @OneToMany Schedule与每个关系 CompanyEmployee ,但是我需要放置 Schedule 的实例都到CompanyEmployee每次,这种方式看起来都不对,现在我也不清楚如何取回它。因此,我们将不胜感激。


这篇文章已更新以展示我的真实场景,而不仅仅是类的通用 Entity1、Entity2、Entity3 名称。

更新 2

我接受了答案,但如果 Schedule 包含列表,我将无法使用它。按照我的计划,Schedule应包含 List<Vacation>知道 Vacations 的集合一年多了,ListDays ,每个显示特定工作日的开始、休息和这一天的结束。那些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;

如何正确包含这些列表?

最佳答案

我想建议以下解决方案。

一个可嵌入类,其中包含用于特定计划的CompanyEmployee

@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;
}

CompanyEmployee 类(未对它们进行任何更改)

@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 相关。不安排

@Entity
public class Day {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;


@ManyToOne
@JoinColumn(name = "vacation_id")
Vacation vacation;
}

希望这对您有所帮助。

关于java - Hibernate 两父一子映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34302585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com