JPA继承 [英] JPA Inheritance

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

问题描述

您好我是JPA的新手,我无法理解它是如何处理继承的。



我有一个特殊的问题,我不需要改变DB方案,但如果你找不到解决方案,我将不胜感激针对不同数据库方案的解决方案建议(欢迎使用Hibernate / TopLink解决方案)。



如果我不清楚或者您需要更多信息,请告诉我。

我有这个数据库:

  TABLE水果
Id Varchar(10)主键
大小Varchar(10)
fruit_type Varchar(10)

TABLE Apple
Id Varchar(10)主键外键引用Fruit.Id
Apple_Property Varchar(10)

到目前为止,我的实体看起来像这:

  @Entity 
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = fruit_type,discriminatorType = DiscriminatorType.Char)
@DiscriminatorValue(value =fruit)

public class Fruit实现Serializable {

@Id
保护字符串ID;

保护字符串大小;
}

@Entity
@DiscriminatorValue(value =apple)
// @ PrimaryKeyJoinColumn(name =Id,referencedColumnName =Id)

公共类Apple延伸Fruit实现Serializable {

私有字符串Apple_Property;
}

目前,我可以保持Fruit对象没有问题。
苹果对象只有当它们的Fruit对象还没有被保存时才会持续存在。



如果我试图用一个已经持久化的Fruit对象持久化一个苹果对象:

 水果fruit1 =新水果(1,小); 
Apple apple1 =新Apple(fruit1,red);
provider.create(fruit1);
provider.create(apple1);

我会在JPA试图在Id =1
已存在。



..

解决方案

当使用JPA来持久化一个子对象时(例如,在你的情况下 provider.create(apple1)),一个记录将被插入子表及其所有父表中。因此, provider.create(apple1)会将一条记录插入到Fruit中,并将一条记录插入到Apple表中。在你的例子中,如果你只想保存一个苹果对象,只需调用 provider.create(apple1) 足够 。它会在苹果对象中持久保留水果参考。



顺便提一下,我建议Fruit Table的PK是一个数字类型,并使用 @GeneratedValue 来标记Fruit bean的ID字段。通过这种方式,您可以让数据库为您生成一个ID,并且不再需要在java代码中显式设置它,以避免此ID已存在错误,因为在java代码中设置了一个已经存在的ID。 p>

Hi I'm new to JPA and I'm having trouble understanding how it handles inheritance.

I have a specific problem I need solved without changing the DB scheme, but if you can't find a solution I would appreciate solution suggestions with a different DB scheme (Hibernate/TopLink solutions welcome).

If I was unclear or you need more information, please tell me so. Thanks in advance!

I have this database:

TABLE Fruit
Id Varchar (10) Primary Key
size Varchar (10)
fruit_type Varchar(10)

TABLE Apple
Id Varchar (10) Primary Key Foreign Key references Fruit.Id
Apple_Property Varchar(10)

So far my entities look like this :

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="fruit_type", discriminatorType=DiscriminatorType.Char)
@DiscriminatorValue(value="fruit")

public class Fruit implements Serializable {

    @Id
    protected String Id;

    protected String size;
}

@Entity
@DiscriminatorValue(value="apple")
//@PrimaryKeyJoinColumn(name="Id" , referencedColumnName="Id")

public class Apple extends Fruit implements Serializable {

    private String Apple_Property;
}

Currently I am able to persist Fruit objects without a problem.. Apple objects persist only when their Fruit object hasn't been persisted yet.

If I try to persist an apple object with an already persisted Fruit object :

Fruit fruit1 = new Fruit("1", "Small");
Apple apple1 = new Apple(fruit1, "red");
provider.create(fruit1);
provider.create(apple1);

I will get an error since JPA tries to create a new row on Fruit table with Id="1" which already exists.

..

解决方案

When using JPA to persist a child object (i.e provider.create(apple1) in your case) , a record will be inserted to the child table and all of its parent tables. So provider.create(apple1) will insert a record to the Fruit and a record to the Apple table.

In your example , if you only want to persist an apple object ,just call provider.create(apple1) is enough . It will persist the fruit reference inside the apple object too.

BTW , I suggest the Fruit Table 's PK to be a number type , and uses @GeneratedValue to mark the ID field of the Fruit bean. In this way , you can let the database to generate an ID for you and no longer need to set it explicitly in the java code to avoid this "ID already exist error" because of setting an already existing ID in the java code.

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

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