JPA继承 - 更改实体类型 [英] JPA Inheritance - Change the Entity type

查看:161
本文介绍了JPA继承 - 更改实体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班级

  @Entity 
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name =PERSONTYPE)
@DiscriminatorValue(value =PERSON)
public class Parent {
.......
}


@Entity
@DiscriminatorValue(value =CHILD)
public class Child extends Parent {
.......
}

我有这样的场景:



1-创建一个人 - 然后PERSONTYPE ='PERSON'



2-进入Person页面并通过勾选'Is Child'复选框将其更新为'CHILD'保存之后,必须将Person保存为类型为'CHILD'。



那么如何将实体类型从'PERSON'更改为'CHILD'?



提前致谢

解决方案

我假设您拥有Child Object的唯一属性这就是为什么你要使用继承,正如@ZB Ziet所评论的那样你应该只使用儿童旗帜



解决方案1 ​​



我看到你正在使用单表继承策略,因此您必须修改descriminator字段manulay(通过使用SQL查询)并在子表上设置appropirate字段。

  UPDATE Parent SET PERSONTYPE ='CHILD'WHERE id = 1 

实际上你使用像这样的本地查询

  enityManager.createNativeQuery( UPDATE PERSON SET PERSONTYPE =?,
VERSION = VERSION + 1 WHERE ID =?)
.setParameter(1,'CHILD')
.setParameter(2,personID)
.executeUpdate();

然后你可以使用entitymanager来获取孩子并设置属性



entityManager.find(Child.class,1).childProp = xxxx



**解决方案2 **



IMO,这里最好的办法是使用连接表策略

在连接表策略中新表为每个具有id作为父实体的外键的子节点创建wholeis。所以更改id也将设置其父节点

  @Entity 
@Inheritance(strategy = InheritanceType.JOINED)
public class Person {
...

如果您可以创建新的子设置id与父设备相同并保存,那将会很酷。 Child c = new Child(); c.setId(parent.getId()); entityManager.merge(c)。但是hibernate试图重新创建一个父对象,导致id confilict。



因此解决方案是编写本机查询
em.createNativeQuery(INSERT into child(id) )VALUES(1))。executeUpdate(); // 1是父ID



关于继承的更多参考资料


I have two classes

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="PERSONTYPE")
@DiscriminatorValue(value="PERSON")
public class Parent {
  .......
}


@Entity
@DiscriminatorValue(value="CHILD")
public class Child extends Parent{
  .......
}

The scenario i have :

1- create a person -- then the PERSONTYPE = 'PERSON'

2- go the Person page and update it to be 'CHILD' by checking a check box 'Is Child' then after save the Person must be saved to be with type 'CHILD'.

Then How can change the entity type from 'PERSON' to 'CHILD'?

Thanks in advance

解决方案

i assume you have unique properties for Child Object and thats why you want to use inheritance, otherwize just as @ZB Ziet commented you should just use child flag

Solution 1

i see you are using single table inheritance strategy, thus you have to modify the descriminator field manulay (by using SQL queries) and set appropirate fields on child table.

UPDATE Parent SET PERSONTYPE='CHILD' WHERE id = 1

practicaly you use native queries like this

enityManager.createNativeQuery("UPDATE PERSON SET PERSONTYPE = ?, "
" VERSION = VERSION + 1 WHERE ID = ?")
.setParameter(1, 'CHILD')
.setParameter(2,personID)
.executeUpdate();

then you can use entitymanager to get the child and set properties

entityManager.find(Child.class,1).childProp=xxxx

** Solution 2 **

IMO, The best thing to do here is instead of using single table strategy you should use joined table strategy

in joined table strategy new table entireis is created for each child having thier id as foreign key to parent entity.so changing the id will also set its parent

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Person { 
...

it would have been cool if you can just create new child set the id same as parent and save. Child c = new Child(); c.setId(parent.getId()); entityManager.merge(c) . but hibernate tries to re-create a parent object resulting id confilict.

so the solution is to write native query em.createNativeQuery("INSERT into child (id) VALUES (1)").executeUpdate(); //1 being the parent id

more reference on inheritance

这篇关于JPA继承 - 更改实体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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