在保持id(休眠)的同时更改/切换子实体

时间:2017-08-03 10:40:05

标签: mysql hibernate

情况

  • Payment.class是抽象父类
  • Credit.class和Cash.class都从Payment
  • 继承
  • UML图附在下面

enter image description here

问题描述

让我们说顾客想要通过现金支付 - >现金对象已创建并持久保存到数据库(通过自动增量分配ID)。现在,她发现她没有足够的现金,因此选择了信用卡付款。使用Hibernate对此进行建模的最佳方法是什么?

当前方法

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Payment {
    @Id
    @GeneratedValue
    protected Long id;
}


@Entity
public class Credit extends Payment {
    // Credit-specific fields
}

@Entity
public class Cash extends Payment {
    // Cash-specific fields
}

通过这种方法,我发现在保留id时无法更改实体类型 - 根本不起作用。我认为我目前的建模方法不是最重要的任务,你有更好的解决方案吗?

0 个答案:

没有答案