本文介绍了如何实现3多对多与Hibernate的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是100%肯定这只是一个Hibernate问题,因为这可能是一个更抽象的决定,但我会试试。

由于问题描述有点长,首先声明我想做的是看看是否可以将实现更改为更类似于最佳实践实现的实现,然后这样。

I'm not 100% sure this is only a Hibernate issue as this might be a more abstract decision but I'll give it a try.
Since the problem description is a bit lengthy I'll first state that what I'd like to do is see if I can change the implementation to something which more resembles a Best practice implementation then this.

我有3个实体,与此问题相关:工作站(ws),员工和组织单位(组织单位)。

员工可以属于一个组织单位。

组织单位可以容纳许多员工。

工作站用于显示组织单位(一般),组织单位及其中的特定员工以及不属于组织的员工的数据-unit。

I have 3 entities, relevant to this issue: Workstation (ws), Employee and Organization-unit(org-unit).
An Employee can belong to one org-unit.
An Org-unit can hold many employees.
A Workstation is used to display data of an Org-unit(in general), Of an Org-unit and a specific employee in it and an employee which does not belong to an org-unit.

目前,由于 不受我控制的各种原因,我们不使用Hibernate中的实体之间的任何关联或通过DB约束,但我们只是使用逻辑上作为外键的列。

我们当前有一个附加的表,它有4列:Id,WSId,EmployeeId,OrgUnitId。

允许WS指向没有组织单位(orgunitId为null)或员工和组织单位(其中没有null)的员工的orgunit(其中employeeId为null)。

Currently, for various reasons which were out of my control, we do not use any associations between the entities in Hibernate or via DB-Constraints but we just use Columns which logically serve as Foreign-Keys.
We currently have an additional table which has 4 columns:Id,WSId,EmployeeId,OrgUnitId.
This allows a WS to refer to an orgunit (where employeeId is null),to an employee without an org-unit (orgunitId is null) or to an employee and org-unit (where none are null).

我想知道:

1.给出一个WS,哪些员工是遵循的,哪些组织单位和如何(即,单独,与雇员?哪个?)

2.生成一个员工,WS正在监控它。

3.生成一个组织单位,WS正在监控它,以及如何(即单独,与一个雇员?)

这个问题涉及表示层,因为它决定视图将被生成,但它是域模型的一部分,作为用户,将使用一个接口来操纵这些监视映射等,这些映射是领域模型的一部分。

I'd like to be able to know:
1.Given a WS, which employees is it following and which org-units and how (i.e., alone, with an employee? which?)
2.Given an employee, which WS are monitoring it.
3.Given an org-unit, which WS are monitoring it and how (i.e., alone, with an employee? which?)
This issues relates to the Presentation layer as it dictates the view will be generated BUT it is a part of the domain model as a user, will use, an interface to manipulate these monitoring mappings and so these mappings are a part of the domain model.

我不知道我有什么不是选项中最少的邪恶,我会很感激评论和建议。

I'm not sure if what I have is not the least evil among options, and I would greatly appreciate comments and suggestions.

EDIT 从一个答案我认为不够清楚,WS可以显示许多这样的映射的数据

EDIT From one of the answers I think it is not clear enough that a WS can display data for many such mappings at the same time, in a mixture of the above sorts (org-unit, employee etc.)

推荐答案

确定,我不需要, t知道如何在数据库侧实现这一点,但这里是一个实体模型,应该涵盖你正在谈论的关系。

OK, I don't know how to implement this on the database side, but here is an Entity Model that should cover the relationship you are talking about.

编辑:
这是一个新版本,以回应您的意见。现在每个WorkStation都有n个绑定,每个绑定都可以有employee或orgunit或两者(使用数据库约束来确保它们没有)。

This is a new version in response to your comments. Now every WorkStation has n bindings each of which can have employee or orgunit or both (use DB constraints to ensure they don't have neither).

绑定每个orgunit和每个雇员,这应该使以上查询更容易:

You can also access the bindings per orgunit and per employee, which should make the above queries much easier:

@Entity
public class OrgUnit{

    @OneToMany(mappedBy="orgUnit")
    private Set<Binding> bindings;

}

@Entity
public class Employee{

    @OneToMany(mappedBy="employee")
    private Set<Binding> bindings;

}

@Entity
public class Binding{

    @ManyToOne(optional = true)
    private Employee employee;

    @ManyToOne(optional=true)
    private OrgUnit orgUnit;

    @ManyToOne(optional=false)
    private WorkStation owner;

}

@Entity
public class WorkStation{

    @OneToMany(mappedBy="owner")
    private Set<Binding> bindings;

}

示例客户端代码:

public Set<WorkStation> getWorkstationsPerEmployee(final Employee employee){
    final Set<WorkStation> workStations = new HashSet<WorkStation>();
    for(final Binding binding : employee.getBindings()){
        WorkStation workStation = binding.getOwner();
        if(workStation!=null)
            workStations.add(workStation);
    }
    return workStations;
}

这篇关于如何实现3多对多与Hibernate的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 09:00
查看更多