Hibernate两个父母一个孩子映射

时间:2015-12-16 01:35:18

标签: java hibernate jpa orm entity

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

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 @ManyToOneCompany建立Employee关系,并且应该遵循以下方式:每个Schedule,因此,List<Schedule>应该只对应CompanyEmployee个实例的一个组合。 如何实现这种关系?

更新1

我只考虑为每个@OneToMany ScheduleCompany添加Employee关系,但我需要将Schedule的实例都添加到CompanyEmployee每次Schedule,这种方式看起来不对,现在对我来说也不是很明显如何取回它。 所以任何帮助都将不胜感激。

此帖子已更新,以显示我所拥有的真实场景,而不仅仅是通用的Entity1,Entity2,类的Entity3名称。

更新2

我接受了答案,但如果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; ,那么如何将这些列表连接到en-GB

eng

如何正确包含这些列表?

1 个答案:

答案 0 :(得分:1)

我想建议以下解决方案。

embeddable类,其中包含特定时间表的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直接相关。不要Schedule

@Entity
public class Day {

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


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

希望这有帮助。