[英]Spring Data Jpa one to many relationship. Not able to insert child records: foreign key is coming as NULL
这是我的父实体
package com.nethum.onboarding.customer.entity;
import java.sql.Timestamp;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.nethum.onboarding.team.entity.Team;
import com.nethum.onboarding.user.entity.User;
@Entity
@Table(name = "CUSTOMER")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class)
public class Customer {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long customerId;
@Column(name = "CUSTOMER_NAME")
private String customerName;
@Column(name = "PARENT_ID")
private Long parentId;
@Column(name = "STATUS")
private String status;
@Column(name = "CUSTOMER_TYPE")
private String customerType;
@Column(name = "HOME_URL")
private String homeUrl;
@Column(name = "SUBDOMAIN")
private String subdomain;
@Column(name = "CREATED_ON")
private Timestamp createdOn;
@Column(name = "CREATED_BY")
private String createdBy;
@Column(name = "CUSTOMER_EMAIL")
private String customerEmail;
@Column(name = "PROVIDER_USER_ID")
private String providerUserId;
@Column(name = "PROVIDER_SRC")
private String providerSrc;
@Column(name = "PROVIDER_NAME")
private String providerName;
@Column(name = "CUSTOMER_CODE")
private String customerCode;
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
private List<CustomerAdmin> customerAdmins;
@OneToMany(mappedBy = "customer")
private List<User> users;
@OneToMany(mappedBy = "customer")
private List<Team> teams;
public Customer() {
}
public Long getCustomerId() {
return customerId;
}
public void setCustomerId(Long customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public Long getParentId() {
return parentId;
}
public void setParentId(Long parentId) {
this.parentId = parentId;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getCustomerType() {
return customerType;
}
public void setCustomerType(String customerType) {
this.customerType = customerType;
}
public String getHomeUrl() {
return homeUrl;
}
public void setHomeUrl(String homeUrl) {
this.homeUrl = homeUrl;
}
public String getSubdomain() {
return subdomain;
}
public void setSubdomain(String subdomain) {
this.subdomain = subdomain;
}
public Timestamp getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Timestamp createdOn) {
this.createdOn = createdOn;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public String getCustomerEmail() {
return customerEmail;
}
public void setCustomerEmail(String customerEmail) {
this.customerEmail = customerEmail;
}
public String getProviderUserId() {
return providerUserId;
}
public void setProviderUserId(String providerUserId) {
this.providerUserId = providerUserId;
}
public String getProviderSrc() {
return providerSrc;
}
public void setProviderSrc(String providerSrc) {
this.providerSrc = providerSrc;
}
public String getProviderName() {
return providerName;
}
public void setProviderName(String providerName) {
this.providerName = providerName;
}
public String getCustomerCode() {
return customerCode;
}
public void setCustomerCode(String customerCode) {
this.customerCode = customerCode;
}
public List<CustomerAdmin> getCustomerAdmins() {
return customerAdmins;
}
public void setCustomerAdmins(List<CustomerAdmin> customerAdmins) {
this.customerAdmins = customerAdmins;
}
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
}
这是我的孩子实体
package com.nethum.onboarding.customer.entity;
import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import com.fasterxml.jackson.annotation.JsonInclude;
@Entity
@Table(name = "CUSTOMER_ADMIN")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class CustomerAdmin {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long customerAdminId;
@Column(name = "ROLE")
private String role;
@Column(name = "STATUS")
private String status;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "CREATED_ON")
private Timestamp createdOn;
@Column(name = "CREATED_BY")
private String createdBy;
@Column(name = "CUSTOMER_EMAIL")
private String customerEmail;
@Column(name = "PROVIDER_USER_ID")
private String providerUserId;
@Column(name = "PROVIDER_SRC")
private String providerSrc;
@Column(name = "PROVIDER_NAME")
private String providerName;
@ManyToOne
@JoinColumn(name = "CUSTOMER_ID")
private Customer customer;
public CustomerAdmin() {
}
public Long getCustomerAdminId() {
return customerAdminId;
}
public void setCustomerAdminId(Long customerAdminId) {
this.customerAdminId = customerAdminId;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Timestamp getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Timestamp createdOn) {
this.createdOn = createdOn;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public String getCustomerEmail() {
return customerEmail;
}
public void setCustomerEmail(String customerEmail) {
this.customerEmail = customerEmail;
}
public String getProviderUserId() {
return providerUserId;
}
public void setProviderUserId(String providerUserId) {
this.providerUserId = providerUserId;
}
public String getProviderSrc() {
return providerSrc;
}
public void setProviderSrc(String providerSrc) {
this.providerSrc = providerSrc;
}
public String getProviderName() {
return providerName;
}
public void setProviderName(String providerName) {
this.providerName = providerName;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
}
当我尝试使用以下数据插入客户和客户管理员时 -
{
"customerName": "TEST",
"homeUrl": "cc.com",
"createdOn": 1472117805000,
"createdBy": "SYSTEM",
"customerEmail": "test@ccc.com",
"providerUserId": "test@cc.com",
"providerSrc": "CC",
"providerName": "CC",
"parentId":1,
"status":"TRIAL",
"customerType":"CUSTOMER",
"customerAdmins": [
{
"firstName": "ADMIN",
"lastName": "ADMIN",
"createdOn": 1472117805000,
"createdBy": "SYSTEM",
"customerEmail": "test@cc.com",
"providerUserId": "test@cc.com",
"providerSrc": "CC",
"providerName": "CC"
}
]
}
我得到错误说 -
2016-08-25 19:05:01,314 [http-nio-8090-exec-1] DEBUG c.n.o.c.r.CustomerRestController - creating customer: com.nethum.onboarding.customer.entity.Customer@11101849[customerId=<null>,customerName=TEST,parentId=1,status=TRIAL,customerType=CUSTOMER,homeUrl=nethum.com,subdomain=<null>,createdOn=2016-08-25 15:06:45.0,createdBy=SYSTEM,customerEmail=test@nethum.com,providerUserId=test@nethum.com,providerSrc=NETHUM,providerName=NETHUM,customerCode=<null>,customerAdmins=[com.nethum.onboarding.customer.entity.CustomerAdmin@43bb2e6e[customerAdminId=<null>,role=<null>,status=<null>,firstName=ADMIN,lastName=ADMIN,createdOn=2016-08-25 15:06:45.0,createdBy=SYSTEM,customerEmail=test@nethum.com,providerUserId=test@nethum.com,providerSrc=NETHUM,providerName=NETHUM,customer=<null>]],users=<null>,teams=<null>]
Hibernate: insert into customer (created_by, created_on, customer_code, customer_email, customer_name, customer_type, home_url, parent_id, provider_name, provider_src, provider_user_id, status, subdomain) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into customer_admin (created_by, created_on, customer_id, customer_email, first_name, last_name, provider_name, provider_src, provider_user_id, role, status) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2016-08-25 19:05:01,431 [http-nio-8090-exec-1] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Column 'CUSTOMER_ID' cannot be null
2016-08-25 19:05:01,467 [http-nio-8090-exec-1] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [/v2] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
org.mariadb.jdbc.internal.util.dao.QueryException: Column 'CUSTOMER_ID' cannot be null
不明白我在做错了什么。
如果我只插入客户数据,它的工作正常。 我认为,因为joinColumn映射在那里它应该获得生成的Id os客户并使用它来插入客户管理员。 让我知道我在这里做错了什么。
我认为没有设置CustomerAdmin
的foreignkey列。
确保将Customer
权利添加到CustomerAdmin
实体。
customerAdminEntity.setCustomer(customerEntity);
然后保存Customer
实体,然后保存CustomerAdmin
实体。
也检查这个问题。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.