子外键引用为NULL。这个外键只是父表的主键。
我有两个表有@OneToMany(Parent)和@ManyToOne(Child)映射。插入到父级中的记录以及子级期望子级中的外键值。
下面的配置中是否有任何遗漏?
package com.springboot.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@Entity
@Table(name = "app_user", catalog = "testdb")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class AppUser implements java.io.Serializable {
private Integer id;
private Rank rank;
private Trainingstatus trainingstatus;
private String name;
private int age;
private double salary;
private Set<Address> addresses = new HashSet<Address>(0);
public AppUser() {
}
public AppUser(Rank rank, Trainingstatus trainingstatus, String name, int age, double salary) {
this.rank = rank;
this.trainingstatus = trainingstatus;
this.name = name;
this.age = age;
this.salary = salary;
}
public AppUser(Rank rank, Trainingstatus trainingstatus, String name, int age, double salary,
Set<Address> addresses) {
this.rank = rank;
this.trainingstatus = trainingstatus;
this.name = name;
this.age = age;
this.salary = salary;
this.addresses = addresses;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "rankId", nullable = false)
//@JsonManagedReference(value="rank")
public Rank getRank() {
return this.rank;
}
public void setRank(Rank rank) {
this.rank = rank;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "trainingStatusId", nullable = false)
//@JsonManagedReference(value="trainingstatus")
/*@JsonBackReference
@JsonIgnore*/
public Trainingstatus getTrainingstatus() {
return this.trainingstatus;
}
public void setTrainingstatus(Trainingstatus trainingstatus) {
this.trainingstatus = trainingstatus;
}
@Column(name = "name", nullable = false, length = 30)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "age", nullable = false)
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
@Column(name = "salary", nullable = false, precision = 22, scale = 0)
public double getSalary() {
return this.salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "appUser" , cascade = CascadeType.ALL)
public Set<Address> getAddresses() {
return this.addresses;
}
public void setAddresses(Set<Address> addresses) {
this.addresses = addresses;
}
}
package com.springboot.model;
// Generated 29 Apr, 2018 9:46:33 PM by Hibernate Tools 4.3.5.Final
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
/**
* Address generated by hbm2java
*/
@Entity
@Table(name = "address", catalog = "testdb")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "addressId")
public class Address implements java.io.Serializable {
private Integer addressId;
private AppUser appUser;
private String text;
private Date lastUpdatedDate;
public Address() {
}
public Address(String text, Date lastUpdatedDate) {
this.text = text;
this.lastUpdatedDate = lastUpdatedDate;
}
public Address(AppUser appUser, String text, Date lastUpdatedDate) {
this.appUser = appUser;
this.text = text;
this.lastUpdatedDate = lastUpdatedDate;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "addressId", unique = true, nullable = false)
public Integer getAddressId() {
return this.addressId;
}
public void setAddressId(Integer addressId) {
this.addressId = addressId;
}
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "id")
@JsonBackReference(value = "id-appUser")
public AppUser getAppUser() {
return this.appUser;
}
public void setAppUser(AppUser appUser) {
this.appUser = appUser;
}
@Column(name = "text", nullable = false, length = 45)
public String getText() {
return this.text;
}
public void setText(String text) {
this.text = text;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "lastUpdatedDate", nullable = false, length = 19)
public Date getLastUpdatedDate() {
return this.lastUpdatedDate;
}
public void setLastUpdatedDate(Date lastUpdatedDate) {
this.lastUpdatedDate = lastUpdatedDate;
}
}
我在github上传了代码:https://github.com/harshalpatil2012/Springboot
答案 0 :(得分:0)
您在父实体@JsonManagedReference
中的addresses
属性上缺少AppUser
注释,这就是为什么使用@JsonBackReference注释的子属性没有获得该值。
这两个注释@JsonManagedReference
和@JsonBackReference
用于处理此父子关系。