简体   繁体   English

Spring Data Jpa一对多的关系。 无法插入子记录:外键为NULL

[英]Spring Data Jpa one to many relationship. Not able to insert child records: foreign key is coming as NULL

Here is my parent entity 这是我的父实体

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);
    }

}

and here is my child entity 这是我的孩子实体

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);
    }

}

When I try to insert customer and customer admin using following data - 当我尝试使用以下数据插入客户和客户管理员时 -

{
  "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"
    }
  ]
}

I get error saying - 我得到错误说 -

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

Don't understand what I am doing wrong here. 不明白我在做错了什么。

If i only insert customer data its working fine. 如果我只插入客户数据,它的工作正常。 I thought since joinColumn mapping is there it should get the generated Id os customer and use that for inserting customer admin. 我认为,因为joinColumn映射在那里它应该获得生成的Id os客户并使用它来插入客户管理员。 Let me know what Am i doing wrong here. 让我知道我在这里做错了什么。

  • I think the foreignkey column in the CustomerAdmin is not set. 我认为没有设置CustomerAdmin的foreignkey列。

  • Make sure that the Customer entitiy is added to the CustomerAdmin entity. 确保将Customer权利添加到CustomerAdmin实体。

    customerAdminEntity.setCustomer(customerEntity);

  • Then save the Customer entity, and then save the CustomerAdmin entity. 然后保存Customer实体,然后保存CustomerAdmin实体。

  • Check this question also. 也检查这个问题。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 外键在 springboot jpa 一对多中以 null 的形式出现 - foreign key coming as null in springboot jpa one to many 在 JPA 一对多关系中分配给 NULL 的外键 - Foreign key assigned to NULL in JPA one-to-many relationship 子表中具有双向“一对多”关系的JHipster外键为Null - JHipster Foreign Key in the child table with a bidirectional One to Many relationship is Null Spring 启动 JPA 一对多双向外键为 null - Spring Boot JPA one-to-many bidirectional foreign key is null sql使用spring jpa更新一对多(外键为空) - sql update one to many using spring jpa (foreign key null) Spring Data Rest:在一对多关系中调用post后,外键更新为null - Spring Data Rest : Foreign key is update with null after post call in one to many relationship Spring Data JPA一对多关系不保存子表 - Spring Data JPA One to Many relationship not saving child table 如何使用休眠映射这些实体? 需要映射一对多关系。 外键未更新 - How can I map these entities using hibernate? Need to map one to many relationship. Foreign key is not updating JPA休眠中一对一关系中的外键约束子级 - foreign key constraint child in One To One relationship in JPA hibernate Spring Data JPA 中的一对多关系 - One-To-Many Relationship in Spring Data JPA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM