父子关系-自我加入映射 [英] Parent - Child relationship - self join mapping

查看:90
本文介绍了父子关系-自我加入映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个应用程序,该应用程序将接收带有雇员列表的XML文件,并将父子/雇员-经理关系存储在单个数据库表中.

I'm trying to build an application which will receive the XML file with list of Employees and store the parent-child/employee-manager relation in the single database table.

我的XML文件如下:

<Employees>
    <Employee manager="Patrick">Martin</Employee>
    <Employee manager="Patrick">Kent</Employee>
    <Employee manager="Martin">Mark</Employee>
    <Employee>Hugo</Employee> <!-- root element of the employee-manager tree -->
    <Employee manager="Hugo">Osa</Employee>
    <Employee manager="Osa">Patrick</Employee>
</Employee>

一个雇员只能有一个经理,但是一个经理可以有多个下属/雇员.

One employee can have only one manager, but one manager can have multiple subordinates/employees.

当解组接收到的XML文件时我没有任何麻烦,但是现在我正在尝试创建适当的模型,该模型将允许我将解组后的值存储在数据库中.数据应存储在名为"Employee"的表中,并应包含以下数据:

I have no troubles when unmarshalling the received XML file but now I'm trying to create the appropriate model which will allow me to store the unmarshalled values in the database. Data should be stored in the table named "Employee" and should contain following data:

------------------------------
| id            | Integer    |
------------------------------
| employee_name | String     |
------------------------------
| parent_id     | Integer    | -- reference to the manager
------------------------------

我创建了一个名为Employee的新类,但不确定如何定义适当的ManyToOne/OneToMany批注.

I created a new class named Employee but I'm not sure how to define appropriate ManyToOne/OneToMany annotations.

由于我对此很陌生,因此我在Google上搜索了一些示例和教程(以及有关Stack Overflow此处类似问题的答案),但是我想我在此实现中犯了一些大错误定义此模型时.我最近的尝试是这样的:

Since I'm fairly new to this, I've Googled couple of examples and tutorials (as well as the answers on the similar questions here on Stack Overflow), but I guess I'm making some big mistake in this implementation when defining this model. My latest try looks like this:

public class Employee {
    @Id
    @GeneratedValue
    private int id;

    @Column(name = "parent_id")
    @Transient
    @ManyToOne(cascade={CascadeType.ALL})
    private String managerName;

    @Column(name = "employee_name")
    @JoinColumn(name="parent_id")
    private String employeeName;

    // getters and setters

如果有人可以向我指出定义适当模型的方向,那将是非常非常感谢!

If anyone could point me in the the direction of defining appropriate model, it would be much, much appreciated!

推荐答案

Hibernate中,当您要映射ManyToOne关系时,您需要在实体之间映射它,而不仅仅是属性,因此您需要引用一个类型的对象Employee,而不仅仅是Stringid.

In Hibernate when you want to map a ManyToOne relationship you map it between entities and not just properties, so you need to reference an object of type Employee and not only a String or an id.

问题:

  • 因此,您的映射不正确,将引发许多映射错误, 而不是写:

  • So your mapping is incorrect and will throw many mapping errors, instead of writing:

@Column(name = "parent_id")
@Transient
@ManyToOne(cascade={CascadeType.ALL})
private String managerName;

您需要像这样映射ManyToOne身份:

You need to map the ManyToOne realtionship like this:

@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="manager_id")
private Employee manager;

  • 并确保您这样映射关系的另一面:

  • And make sure you map the other side of the relationship like this:

    @OneToMany(mappedBy="manager")
    private Set<Employee> subordinates = new HashSet<Employee>();
    

  • 此外,您对列employee_name的映射不正确, @JoinColumn仅用于关系,不能与 简单的专栏,您需要这样写:

  • Also your mapping for the column employee_name is incorrect, the @JoinColumn is only used for relationships and can't be used with a simple column, you need to write it like this:

    @Column(name = "employee_name")
    private String employeeName;
    

  • @Transient在您的映射中无用,我们仅在以下情况下使用它: 希望使用不会在数据库中保留的属性.

  • The @Transient is useless in your mapping, we only use it if we want to use an attribute that won't be persisted in the database.

    最重要确保使用@Entity映射类,这样可以 保留在数据库中.

    And most important make sure you map your class with @Entity, so it can be persisted in the database.

    示例:

    您可以检查 休眠自我连接注释一对多映射示例 ,它使用您要实现的相同模型.

    You can check Hibernate Self Join Annotations One To Many mapping example it uses the same model you want to implement.

    这篇关于父子关系-自我加入映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆