当前位置: 首页 > 知识库问答 >
问题:

JPA:外键未从父主键插入子节点

袁谭三
2023-03-14

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

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

下面的确认中有什么遗漏吗?

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

共有1个答案

曹鹏海
2023-03-14

您缺少父实体AppUser地址属性的@JsonManagedReference注释,这就是用@JsonBackReference注释的子属性无法获取值的原因。

这两个注释,@JsonManaged引用@JsonBack引用,用于处理此父子关系。

 类似资料:
  • 问题内容: 我有一个关于从子实体ir引用ParentEntities的问题,如果我有这样的事情: Parent.java: 和Child.java: 将创建以下表: 好的,到目前为止,一切都很好。但是,当涉及到使用Java的Reference时,我想,您可以做这样的事情。 这导致在数据库中: 但是事实并非如此,您必须明确地将“父级”设置为“子级”(我认为框架可能可以单独执行)。 因此,数据库中真正

  • 在我的jpa映射中,我试图将带有一个主键的父类映射到带有复合键的子类,但似乎插入了错误的表,它已经生成了2个表,但不幸的是我没有绑定外键(policy_value_summary_id) 我试图用策略值(子类)保存策略摘要值(父),如下所示 下面是输出给我的错误

  • 假设您有一个表,如下所示: 可以看到列是表的主键和外键。是的,MySQL成功生成了这个表。 问题很简单,我将如何在JPA实体中映射这一点?我是否应该有映射到列的1个id和连接列的另一个字段?欢迎提出建议。

  • 在一个双向关系中,我不能将子外键与父外键相关联。我有一个父类联盟是这样定义的: 子类ServiceCharge是这样定义的: 然后运行测试代码: 正如您所看到的,在将Union u持久化到数据库之后,我将这两个实体与一个控制器相关联,ServiceController.Add只是通过使用Union setter将实体charge1添加到servicecharges列表中,然后将该实体持久化到数据库

  • 我有2个实体类:Student是父类,Address是子类,具有一对一映射: student_id int(pk),roll_no int(pk),name varchar student_id(pk,fk),国家/地区varchar address实体中的student_id既是主键也是外键 学生实体类: 异常为-org.hibernate.AnnotationException:@mapsid

  • 我想将类的外键从列表映射到类的主键。实体框架在子问题中没有建立这种关系。 我的形式类。 我的问题课。 IEntityTypeConfiguration中的配置应该是什么样子?