如何使用JOINED继承策略和Hibernate创建现有超级对象的子对象 [英] How to create child object of existing super object, using JOINED inheritance strategy and Hibernate

查看:86
本文介绍了如何使用JOINED继承策略和Hibernate创建现有超级对象的子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Hibernate如何创建持久化超级对象的子对象的适当方式是什么?

考虑下面的例子:
在数据库中持久化 User 带ID 1,名字Kevin和laseName Smith 。当时,数据库模型扩展了新的实体 Auditor ,它是 User 的子类。对于继承策略,使用策略 JOINED ,所以数据库模型现在有两个表: user auditor 。这些表使用user_id FK进行连接。



我想创建Kevin Smith对象类型的Auditor并坚持。问题在于操作是事务性的,Hibernate抛出 NonUniqueObjectException 。存在任何方式如何安全地转换持久化对象做子对象?我尝试驱逐给定的用户对象,但仍然是相同的。



用户实体

  @Entity 
@Table(name =user)
@Inheritance(strategy = InheritanceType.JOINED)
public class User {

私人长ID;
private String firstName;
private String lastName;

// getters and setters
}

审计实体

  @Entity 
@Table(name =auditor)
public class Auditor扩展用户{
//一些属性
}

Logic


  public void createAuditorOfUser(final long userId){
final User user = userService.getUserById(userId);
// ...
最终审核员审核员=新审核员();
auditor.setId(user.getId());
auditor.setFirstName(user.getFirstName());
auditor.setLastName(user.getLastName());
userService.evict(user);
//会抛出NonUniqueObjectException
auditorService.update(auditor);
// ...
}


我希望问题很明显,如果没有,我会尝试改进描述。

解决方案

我认为Hibernate有意限制这种行为。当然,你可以用原生的SQL解决方法来做到这一点(就像你已经做过的那样)。



在Java中,我们不能将超类的对象转换为子类然后填写同一对象的新字段。相反,创建一个新的子类实例,然后复制字段。



看起来像一对一关系映射更适合您所需的功能。


what is the appropriate way how create child object of persisted super object with Hibernate?

Consider following example: In database is persisted User with ID 1, firstName Kevin and laseName Smith. By the time is database model extended of new Entity Auditor which is child class of User. For inheritance is used strategy JOINED, so database model now has tow tables: user and auditor. These tables are jointed using user_id FK.

I would like create of Kevin Smith object type Auditor and persist. Problem is that operations are transactional and Hibernate throws NonUniqueObjectException. Exists any way how to safely cast persisted object do child object? I tried to evict given User object, but still the same.

User entity

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

    private Long id;
    private String firstName;
    private String lastName;

    // getters and setters
}

Auditor entity

@Entity
@Table(name = "auditor")
public class Auditor extends User {
  // some properties
}

Logic

public void createAuditorOfUser(final long userId) {
  final User user = userService.getUserById(userId);
  // ...
  final Auditor auditor = new Auditor();
  auditor.setId(user.getId());
  auditor.setFirstName(user.getFirstName());
  auditor.setLastName(user.getLastName());
  userService.evict(user);
  // will throw  NonUniqueObjectException
  auditorService.update(auditor);
  // ...
}

I hope the problem is clear, if not I'll try improve description.

解决方案

I think Hibernate intentionally restrict such behaviour. Of course you can do it with native SQL workaround (as you already did).

In Java also we can't cast an object of super class to a sub-class then fill new fields of same object. Instead, create a new instance of sub-class then copy fields.

Seems like one-to-one relation mapping fits better for the functionality you required.

这篇关于如何使用JOINED继承策略和Hibernate创建现有超级对象的子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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