Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate JPA:外键未从父主键插入子项_Hibernate_Jpa_Spring Data Jpa_Hibernate Mapping - Fatal编程技术网

Hibernate JPA:外键未从父主键插入子项

Hibernate JPA:外键未从父主键插入子项,hibernate,jpa,spring-data-jpa,hibernate-mapping,Hibernate,Jpa,Spring Data Jpa,Hibernate Mapping,子外键引用为空。这个外键只是父表的主键 我下面有两个表,其中有@OneToMany(父)和@manytone(子)映射。记录插入到父项以及子项中,子项中的外键值除外 以下确认中是否缺少任何内容 package com.springboot.model; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persi

子外键引用为空。这个外键只是父表的主键

我下面有两个表,其中有@OneToMany(父)和@manytone(子)映射。记录插入到父项以及子项中,子项中的外键值除外

以下确认中是否缺少任何内容

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

    }
package com.springboot.model;
导入javax.persistence.CascadeType;
导入javax.persistence.Column;
导入javax.persistence.Entity;
导入javax.persistence.FetchType;
导入javax.persistence.GeneratedValue;
导入静态javax.persistence.GenerationType.IDENTITY;
导入java.util.HashSet;
导入java.util.Set;
导入javax.persistence.Id;
导入javax.persistence.JoinColumn;
导入javax.persistence.manytone;
导入javax.persistence.OneToMany;
导入javax.persistence.Table;
导入com.fasterxml.jackson.annotation.JsonBackReference;
导入com.fasterxml.jackson.annotation.JsonIdentityInfo;
导入com.fasterxml.jackson.annotation.JsonIgnore;
导入com.fasterxml.jackson.annotation.JsonManagedReference;
导入com.fasterxml.jackson.annotation.ObjectiveGenerators;
@实体
@表(name=“app\u user”,catalog=“testdb”)
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class,property=“id”)
公共类AppUser实现java.io.Serializable{
私有整数id;
私人职级;
私人培训状态培训状态;
私有字符串名称;
私人互联网;
私人双薪;

私有集

在父实体
AppUser
中的
地址
属性上缺少
@JsonManagedReference
注释,这就是为什么用@JsonBackReference注释的子属性未获得值的原因


这两个注释,即
@JsonManagedReference
@JsonBackReference
,用于处理此父子关系。

为了解决此问题,我首先在子多对一映射子中保留了该引用集,然后在单独的存储库调用中保留该引用集。