Java JPA-如果父记录已获取,则JPA存储子记录具有父id而不是实体记录
我仍然掌握着JPA的概念,似乎在任何地方都找不到我问题的答案 假定 这两个类都用@GeneratedValue(strategy=GenerationType.IDENTITY)进行注释,它们都有getter和setter 正如您所看到的Java JPA-如果父记录已获取,则JPA存储子记录具有父id而不是实体记录,java,spring-boot,jpa,spring-data-jpa,spring-boot-2,Java,Spring Boot,Jpa,Spring Data Jpa,Spring Boot 2,我仍然掌握着JPA的概念,似乎在任何地方都找不到我问题的答案 假定 这两个类都用@GeneratedValue(strategy=GenerationType.IDENTITY)进行注释,它们都有getter和setter 正如您所看到的child2只有“parent”:1,因为child1首先映射到该父级! 类似地,child4只有父项:2,因为child3首先映射到该父项 有人能解释一下这种行为吗?我尝试了在父对象上使用fetch=FetchType.EAGER,但没有效果! 我希望所有的孩
child2
只有“parent”:1
,因为child1
首先映射到该父级!
类似地,child4
只有父项:2
,因为child3
首先映射到该父项
有人能解释一下这种行为吗?我尝试了在父对象上使用fetch=FetchType.EAGER,但没有效果!
我希望所有的孩子都有一个全面的父对象,以防止另一个DB trip
提前谢谢
用实际课程更新问题:
家长
package backend.application.payroll.models;
import com.fasterxml.jackson.annotation.*;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
@Data
@Entity
@Table(name = "employees")
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Employee implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "emp_code", nullable = false)
private String empCode;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "middle_name", nullable = true)
private String middleName;
@Column(name = "last_name", nullable = false)
private String lastName;
@Column(name = "dob", nullable = false)
@Temporal(TemporalType.DATE)
private Date dob;
@Column(name = "id_number", nullable = true)
private String idNumber;
@Column(name = "passport_number", nullable = true)
private String passportNumber;
@Column(name = "email_address", nullable = true)
private String emailAddress;
@OneToOne(cascade = CascadeType.DETACH)
@JoinColumn(name = "pay_grade", referencedColumnName = "id", nullable = true)
private Salary payGrade;
@Column(name = "basic_pay", nullable = true)
private BigDecimal basicPay;
@OneToOne(cascade = CascadeType.DETACH)
@JoinColumn(name = "department", referencedColumnName = "id", nullable = true)
private Department department;
@OneToOne(cascade = CascadeType.DETACH)
@JoinColumn(name = "position", referencedColumnName = "id", nullable = true)
private Position position;
@Column(name = "tax_number", nullable = true)
private String taxNumber;
@Column(name = "hire_date", nullable = true)
@Temporal(TemporalType.DATE)
private Date hireDate;
@Column(name = "address1", nullable = true)
private String address1;
@Column(name = "address2", nullable = true)
private String address2;
@Column(name = "postal_code", nullable = true)
private String postalCode;
//country
@Column(name = "phone_number", nullable = true)
private String phoneNumber;
//banking details
//HERE IT WORKS FINE SINCE IT'S ONETOONE - YOU CAN IGNORE
@OneToOne(mappedBy = "employee")
//@JsonManagedReference//used in conjunction with @JsonBackReference on the other end - works like @JsonIdentityInfo class annotation.
private User user;
//THIS IS WHAT CAUSING THE PROBLEM
@OneToMany(mappedBy = "owner", fetch = FetchType.LAZY)
//@JsonBackReference
@JsonIgnore
private Set<Costcentre> costcentres = new HashSet<>();
public Employee() {
}
}
将
JsonIdentityInfo
添加到父级和子级,您可以在父级上添加fetch=FetchType.EAGER
,并将JsonIgnore
忽略获取子级和父级
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
,例如:
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
Parent{
....
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
@JsonIgnore
Collection<Child> children;
....
}
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
Child{
...
@JoinColumn(name = "parent", referencedColumnName = "id", nullable = true)
@ManyToOne(optional = false, fetch = FetchType.EAGER)
Parent parent;
...
}
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class,property=“id”)
母公司{
....
@OneToMany(mappedBy=“parent”,fetch=FetchType.LAZY)
@杰索尼奥雷
收集儿童;
....
}
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class,property=“id”)
孩子{
...
@JoinColumn(name=“parent”,referencedColumnName=“id”,nullable=true)
@ManyToOne(可选=false,fetch=FetchType.EAGER)
父母;
...
}
谢谢您的回复@sc0der我已经有了这些注释,除了fetch=FetchType.EAGER
但是即使这样也不能解决问题。问题依然存在。我甚至想放弃我的整个数据库,因为我只是在后来添加了这种关系,但我怀疑这会有帮助(我认为JPA足够聪明)。我想我做的唯一一件事就是扔掉儿童桌,以确保FK的适当娱乐,但即使这样,我也没有得到任何运气!我之前尝试过实现Serializable
,它没有改变任何东西,我将其删除。我刚把它放回去,结果是一样的。很明显,有什么东西阻止了对父对象的多次检索,我想知道这是什么!事实上,我甚至不认为这与多次检索有关,这只是嵌入以前检索到的实体的结果的问题!JSON问题?太令人困惑了!检查这一条:还有这一条:我真的遇到了这些文章,并尝试了回来,管理了引用,但没有成功!用实际的两个类更新我的问题可以吗?(希望合适)
package backend.application.payroll.models;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "costcentres")
@Data
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Costcentre implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "description", nullable = true)
private String description;
@ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.EAGER)
@JoinColumn(name = "owner", referencedColumnName = "id", nullable = true)
//@JsonManagedReference
private Employee owner; //CULPRIT
public Costcentre() {
}
public Costcentre(long id, String name, String description) {
super();
this.id = id;
this.name = name;
this.description = description;
}
}
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
Parent{
....
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
@JsonIgnore
Collection<Child> children;
....
}
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
Child{
...
@JoinColumn(name = "parent", referencedColumnName = "id", nullable = true)
@ManyToOne(optional = false, fetch = FetchType.EAGER)
Parent parent;
...
}