Существует 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