Hibernate:将基类的实例更改为子类 [英] Hibernate: Change instance of base class to subclass

查看:105
本文介绍了Hibernate:将基类的实例更改为子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个具体的超类更改为它的一个子类。我已经在下面列出了一个例子:

$ p $ @Entity
@Table(name =employees)
@Inheritance(strategy = InheritanceType.JOINED)
public class Employee {

@Id
@Column(name =id)
private String id;

public Employee(String id){
this.id = id;
}
...
}

@Entity
@Table(name =managers)
@PrimaryKeyJoinColumn(name = id,referencedColumnName =id)
public class Manager扩展Employee {

public Manager(String id){
super(id);




$ b @Entity
@Table(name =history)
public class历史{
...
/ **
*
* /
@ManyToOne
@JoinColumn(name =employee_id)
私人雇员雇员;
...
}

我正在使用的三个类是员工,经理和历史。所有经理都是员工,但并非所有员工都是经理。所有员工(和经理)都有历史记录。员工可能会晋升到管理层。发生这种情况时,员工的历史记录应保留为保持其员工编号相同。这将允许很容易找到经理就职的历史。



实施促销操作由于数据库的限制而变得复杂:数据库不允许删除旧的员工和创建一个具有相同ID的新管理器,而无需通过级联操作删除所有的历史对象 - 人力资源部门不允许这样做,否则我的工作将变得简单!



有可能将管理器(新管理器)行添加到现有员工而不使用自定义SQL操作?





  public void promote(Employee employee){
/ *将员工的身份证复印给经理。无法工作,因为ID已存在的员工* /
经理经理=新经理(employee.getId());
this.entityManager.persist(manager);
}

...或...

  public void promote(Employee employee){
/ *分离雇员然后合并。无法工作:再次失败,使用NonUniqueObject Exception * /
this.entityManager.detach(employee);
经理经理=新经理(employee.getId());
this.entityManager.merge(manager);
}

我该如何完成这项工作?我甚至在正确的轨道上用 detach() merge()

/ JPA?



任何想法在这一刻都会有所帮助,因为我很难过!


  • Aaron


解决方案

在这里重新划船。不幸的是,它看起来像你有一个典型的例子使用继承来表示角色。 Hibernate - 一个 Object -Relational Mapper - 和一个 Object - 面向语言 - 都会在这一个上面与你对抗,因为你的对象模型是错误的。我会说你最好的选择是现在用重构和数据迁移来修复它。最终结果应该是每个人都是一名员工,而一些员工与一个或多个部门或其他员工有某种管理关系。


I would like to change a concrete superclass to one of its subclass. I've included an example below:

@Entity
@Table(name = "employees")
@Inheritance(strategy = InheritanceType.JOINED)
public class Employee {

    @Id
    @Column(name = "id")
    private String id;

    public Employee( String id ) {
        this.id = id;
    }
 ...
}

@Entity
@Table(name = "managers")
@PrimaryKeyJoinColumn(name = "id", referencedColumnName = "id")
public class Manager extends Employee {

    public Manager( String id ) {
        super(id);
    }
 ...
}


@Entity
@Table(name = "history")
public class History {
 ...
/**
 * 
 */
@ManyToOne
@JoinColumn(name = "employee_id")
private Employee employee;
 ...
}

The three classes I'm working with are Employee, Manager and History. All Managers are Employees, but not all Employees are Managers. All Employees (and Managers) have a History. Employees may be promoted to Management. When this happens an Employee's history should be retained by keeping their Employee ID the same. This will allow a Manager's History through employment to be easily found.

Implementing the promotion operation is complicated by constraints withing the database: the database will not allow removing the old Employee and creating a new Manager with the same ID without removing all of the History objects by cascading operation - which Human Resources won't allow, otherwise my job would be easy!

Is it possible to add or attach the Manager (new managers) row to an existing Employee without resorting to custom SQL operation?

I've tried the following solutions without success:

public void promote( Employee employee ) {
    /* copy over the ID of the employee to the manager.  Will not work because employee with ID already exists */
    Manager manager = new Manager(employee.getId());
    this.entityManager.persist( manager );
}

... or ...

public void promote( Employee employee ) {
    /* detach the employee then merge.  Will not work: fails, again, with a NonUniqueObject Exception */
    this.entityManager.detach( employee );
    Manager manager = new Manager(employee.getId());
    this.entityManager.merge( manager );
}

How can I get this done? Am I even on the right track with detach() and merge()?

Is this possible in Hibernate/JPA?

Any ideas will be helpful at this point as I'm stumped!

  • Aaron

解决方案

As you're no doubt starting to see, you're rowing against the wind here. Unfortunately, it looks like you have a classic example of using inheritance to represent role. Both Hibernate--an Object-Relational Mapper--and Java--an Object-oriented language--are going to fight you on this one because your object model is wrong. I'd say your best bet is to fix it now with a refactoring and data migration. The end result should be that everyone is an Employee, and some Employees have some kind of "manages" relationship with one or more departments or other Employees.

这篇关于Hibernate:将基类的实例更改为子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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