大佬教程收集整理的这篇文章主要介绍了在 Spring Data JPA 中,如何通过调用外部实体实例上的方法来创建和持久化内部实体,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 Spring Boot 应用程序,它使用 Spring Data JPA 并包含一个实体层次结构,其中父 Pod
实体在逻辑上拥有子 Pea
实体的链接列表。
父 Pod
实体基本上是这样的:
package com.github.oIE.entitIEs;
import lombok.Getter;
import javax.persistence.*;
import java.util.UUID;
@Entity
@Getter
public class Pod {
@ID
@GeneratedValue
private UUID ID;
@OnetoOne(fetch = FetchType.EAGER)
private Pea latestPea;
public Pea nextPea() {
// Todo:
//this.latestPea = new Pea(latestPea);
//persist(latestPea) // <<-- I don't kNow how to implement this
//return latestPea;
}
}
目标是通过在 Pea
实例上调用方法 nextPea
来允许创建和持久化子 Pod
实体。
这个场景中的 Pea
类是这样的:
package com.github.oIE.entitIEs;
import lombok.Getter;
import javax.persistence.*;
import java.util.UUID;
@Entity
@Getter
public class Pea {
@ID
@GeneratedValue
private UUID ID;
@OnetoOne(fetch = FetchType.LAZY)
private Pea prevIoUs;
public Pea(Pea prevIoUs) {
this.prevIoUs = prevIoUs;
}
public Pea() {
}
}
所需的用例是这样的:
package com.github.oIE;
import com.github.oIE.entitIEs.Pea;
import com.github.oIE.entitIEs.Pod;
import org.springframework.boot.CommandlineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.persistence.Entitymanager;
import javax.persistence.PersistenceContext;
import javax.@R_675_7330@ction.@R_675_7330@ctional;
@SpringBootApplication
public class OuterInnerEntityApplication implements CommandlineRunner {
@PersistenceContext
private Entitymanager entitymanager;
public static voID main(String[] args) {
SpringApplication.run(OuterInnerEntityApplication.class,args);
}
@OverrIDe
@@R_675_7330@ctional
public voID run(String... args) throws Exception {
Pod pod = new Pod();
entitymanager.persist(pod);
Pea pea = pod.nextPea(); // <<-- this should both create and persist a new `Pea`
}
}
将 Entitymanager
的实例传递给 nextPea
似乎不合理,因为不应要求调用者拥有它。
实施这种安排的“正确”方式可能是什么?
附言
// build.gradle
plugins {
ID 'org.springframework.boot' version '2.4.5'
ID 'io.spring.dependency-management' version '1.0.11.RELEASE'
ID 'java'
}
group = 'com.github'
version = '0.1.0-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compiLeonly {
extendsFrom AnnotationProcessor
}
}
repositorIEs {
mavenCentral()
}
dependencIEs {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2'
compiLeonly 'org.projectlombok:lombok'
AnnotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
// setTings.gradle
rootProject.name = 'outer-inner-entity'
您可以尝试在 persist
实体中对 Pod
操作使用级联:
@Entity
@Getter
public class Pod {
@Id
@GeneratedValue
private UUID id;
@OneToOne(cascade = CascadeType.PERSIST)
@Joincolumn(name = "pea_id")
private Pea latestPea;
public Pea nextPea() {
this.latestPea = new Pea(latestPea);
return latestPea;
}
}
在您在 Pea
方法中持久化 Pod
实体后,这将自动持久化 run
实体。您只需在持久化 Pea
之前创建并将 Pod
设置为 Pod
:
@Override
@@R_675_7330@ctional
public void run(String... args) throws Exception {
Pod pod = new Pod();
Pea pea = pod.nextPea();
entitymanager.persist(pod);
}
以上是大佬教程为你收集整理的在 Spring Data JPA 中,如何通过调用外部实体实例上的方法来创建和持久化内部实体全部内容,希望文章能够帮你解决在 Spring Data JPA 中,如何通过调用外部实体实例上的方法来创建和持久化内部实体所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。