JPA - 在 @OneToMany 关系中,外键未保存在数据库中

问题描述 投票:0回答:0

出于某种原因我有两个实体,人和地址 person_ id 在数据库中总是空的并且没有被保存,任何猜测为什么会这样

 @Entity
    public class Person {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private int id;
        
        private String name;
    
        @OneToMany(mappedBy = "personId", cascade = CascadeType.ALL,orphanRemoval = true)
        List<Address> address;
}

    @Entity
    public class Address {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private int id;
    
        private String city;
    
        private String state;
    
    
        private String country;
    
        @ManyToOne
        @JoinColumn(name = "person_id", referencedColumnName = "id" , nullable = false,updatable = true, insertable = true)
        private Person personId;
}

 @PostMapping("/person")
    Person postPerson(@RequestBody Person person){
        return personService.save(person);
    }

hibernate spring-data-jpa nhibernate-mapping hibernate-criteria
© www.soinside.com 2019 - 2024. All rights reserved.