@OneToMany关系不会将父级的主键保存在子级表中 [英] @OneToMany relationship does not save the primary key of the parent in the children tables

查看:165
本文介绍了@OneToMany关系不会将父级的主键保存在子级表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们有三个JPA实体.一个Person和两个一对多关系.当我尝试保存Person AD_P_IDAC_P_ID外键时,它们始终为null. 这些字段的期望值是个人ID.我在做什么错了?

Let there be three JPA Entities. A Person and two one-to-many relations to it. When I'm trying to save the Person AD_P_ID and AC_P_ID foreign keys are always null. The expected value for these fields is the person Id. What am I doing wrong?

  1. Person.java:它包含与AccountAddress实体类的一对多关系:

  1. Person.java: This contains one to Many relationships with Account and Address Entity classes:

@Entity
@Table(name = "A2C_PERSON")
class Person implements Serializable {

    private long id;

    private List<Account> acs;

    private List<Address> ads;

    @OneToMany(cascade=CascadeType.ALL, mappedBy = "person")
    public List<Account> getAccount() {
        return this.acs;
    }

    @OneToMany(cascade=CascadeType.ALL, mappedBy = "person")
    public List<Address> getAddress() {
        return this.ads;
    }
}

  • Account.java

  • Account.java

    @Entity
    @Table(name = "A2C_ACCOUNT")
    public class Account implements Serializable {
    
        private long id;
    
        private Person person;
    
        @ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
        @JoinColumn(name = "AC_P_ID")
        public Person getPerson() {
            return this.person;
        }
    }
    

  • Address.java

  • Address.java

    @Entity
    @Table(name = "A2C_ADDRESS")
    public class Address implements Serializable {
    
        private long id;
    
        private Person person;
    
        @ManyToOne(cascade=CascadeType.ALL,fetch = FetchType.LAZY)
        @JoinColumn(name = "AD_P_ID")
        public Person getPerson() {
            return this.person;
        }
    }
    

  • 用于保存人员的代码:

    Person p = new Person();
    
    Account ac1 = new Account();
    Account ac2 = new Account();
    List<Account> acList = new ArrayList<>();
    acList.add(ac1);
    acList.add(ac2)
    
    Address ad1 = new Adddress();
    Address ad2 = new Adddress();
    List<Address> adList = new ArrayList<>();
    acList.add(ad1);
    acList.add(ad2)
    
    p.setAcs(acList);
    p.setAds(adList);
    
    personRepo.save(p);
    

    推荐答案

    对于每个AddressAccount实体,您需要设置Person实体. 为了使休眠状态将ID保存在子级中,这是强制性的:

    For each Address and Account entity you need to set the Person entity. This is mandatory in order for hibernate to save the ids in children:

    Person p = new Person();
    
    Account ac1 = new Account();
    ac1.setPerson(p);  
    List<Account> acList = new ArrayList<>();
    acList.add(ac1);
    
    Address ad1 = new Adddress();
    ad1.setPerson(p);
    List<Address> adList = new ArrayList<>();
    acList.add(ad1);
    

    这篇关于@OneToMany关系不会将父级的主键保存在子级表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆