Java 如何使用JPA持久化具有单一类型多个字段的实体?

Java 如何使用JPA持久化具有单一类型多个字段的实体?,java,postgresql,jpa,foreign-keys,Java,Postgresql,Jpa,Foreign Keys,背景: id [PK] | batch_id | brigade_id | call_sign | lat | lon | location | type_id 我有一个抽象类MobileResource,它包含三个字段,每个字段的类型都是Site。这些字段可以而且通常包含相同的对象 @Entity public abstract class MobileResource extends Resource implements Serializable { @ManyToOne(fe

背景:

id [PK] | batch_id | brigade_id | call_sign | lat | lon | location | type_id
我有一个抽象类
MobileResource
,它包含三个字段,每个字段的类型都是
Site
。这些字段可以而且通常包含相同的对象

@Entity
public abstract class MobileResource extends Resource implements Serializable {

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    private Site homeSite;
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    private Site currentSite;
    @ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST})
    private Site relocationSite;

    // constructors, getters, setters
}
我还有一个
Person
的具体子类
MobileResource

@Entity
public class Person extends MobileResource implements Serializable {

    private String firstName;
    private String surname;

    // constructors, getters, setters
}
所讨论的模块是一个使用spring数据jpaapostgres数据库的spring boot应用程序。数据库模式由spring/hibernate自动生成

问题:

id [PK] | batch_id | brigade_id | call_sign | lat | lon | location | type_id
我正在尝试持久化从外部REST资源检索的
Person
的新实例。当我使用spring boot injected
PersonRepository
扩展
JpaRepository
,并调用
PersonRepository.save(person)
,我得到以下异常:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-06-07 10:36:30,383 ERROR SpringApplication - Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
    ... 6 more
Caused by: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [fk_rmnbkmxocx4dcayhiyr1wxypc]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
    ... 23 more
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
    ... 56 more
Caused by: org.postgresql.util.PSQLException: ERROR: insert or update on table "person" violates foreign key constraint "fk_rmnbkmxocx4dcayhiyr1wxypc"
  Detail: Key (current_site_id)=(738) is not present in table "site".
    ... 62 more

id [PK] | batch_id | brigade_id | call_sign | lat | lon | incident_id | type_id | current_site_id | home_site_id | relocation_site_id | status_id | first_name | surname
网站
栏目:

id [PK] | batch_id | brigade_id | call_sign | lat | lon | location | type_id
问题:

id [PK] | batch_id | brigade_id | call_sign | lat | lon | location | type_id
应该进行什么样的更改才能使持久化工作正常进行。我的第一个假设是模式需要更改,但我不确定它将以何种方式进行更改,也不确定如何使用注释实现这一点

非常感谢

编辑:只是为了澄清一下,我希望在持久化
Person
对象时能够持久化
Site
对象,而不是为
Person
预先持久化任何内容

我的一个想法是使用远程服务器的主键(
id
)作为备用键,并使用
@GeneratedValue
生成我自己的键,但我认为这也可能导致同样的问题


有人知道为什么在这种情况下,
CascadeType.PERSIST
不起作用吗?

似乎站点表中没有id=738的行,所以无法将该行插入Person表


您应该首先尝试在id为738的站点表中插入一行,然后再尝试插入Person表。

我正在通过REST服务器以XML格式检索
Person
对象。此XML包含
站点
对象本身,以及它们与
的关系。为了使用此方法,我必须持久化此XML的对象树中包含的每个对象,其中有许多对象。我希望persist将级联到
站点
对象?