java @OnetoMany关系只接受一个对象而不是列[SPRING]中的列表

m1m5dgzv  于 2023-02-11  发布在  Java
关注(0)|答案(2)|浏览(95)

我在处理两个属性(主人和宠物:一个主人应该可以有许多宠物)在Spring,这是我的代码:

@Entity
@Table(name="owners_table")
public class Owners {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    
    @Column(nullable=false)
    private String name;
    
    @Column(nullable=false)
    private String phoneNumber;
    
    @Column(nullable=true)
    private String email;
    
    @Column(nullable=false)
    private String address;
     
    @OneToMany(fetch=FetchType.LAZY,mappedBy="id")
    @JsonManagedReference(value="pet-owners")
    @JsonIgnore
    private List<Pets> pets;

    
    public Owners() {
        
    }
    @JsonCreator
    public Owners(Integer id, String name, String phoneNumber, String email, String address) {
        this.id = id;
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.email = email;
        this.address = address;
    }

(plus吸气器和设置器)

@Entity
@Table(name="pets_table")
public class Pets {
    private String name;
    private String breed;
    private Date birthDate;
    
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JsonBackReference(value="pet-owners")
    @JoinColumn(name = "owner_id")
    private Owners owner;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    
    
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name="walk_id")
    @JsonBackReference(value="pet-walks")
    private DogWalking walk;
    

    public Pets() {
    }
    
    @JsonCreator
    public Pets(String name, String breed, Date birthDate,  Owners owner , Integer id) {
        this.name = name;
        this.breed = breed;
        this.birthDate = birthDate;
        this.id = id;
    }

(plus吸气器和设置器)
我相信这是代码中唯一对这个问题有意义的部分,但是如果您需要任何其他东西,可以告诉我,我会把它放在这里。

**现在我来解释一下这个问题:**我的数据库里有这个所有者:(本人要求:本地主机:8080/所有者/6)

{
        "id": 6,
        "name": "Lucas Calado",
        "phoneNumber": "(83) 98326-9273",
        "email": "lucasthecalado@gmail.com",
        "address": "Rua Bancário João Luiz , 92",
        "pets": []
}

然后我在宠物列表里加了一个宠物(本人要求:本地主机:8080/宠物/插入)

{
                "name": "Fenrir",
                "breed": "Husky siberiano",
                "birthDate": "2017-03-08T03:00:00.000+00:00",
                "owner":{
                    "id":6
                }
                "walks": null
}

直到现在,一切都很好

现在,如果我发出同样的请求,通过id获取所有者,则会显示以下内容:

{
        "id": 6,
        "name": "Lucas Calado",
        "phoneNumber": "(83) 98326-9273",
        "email": "lucasthecalado@gmail.com",
        "address": "Rua Bancário João Luiz , 92",
        "pets": [
            {
                "name": "Fenrir",
                "breed": "Husky siberiano",
                "birthDate": "2017-03-08T03:00:00.000+00:00",
                "id": 6,
                "walks": null
            }
        ]
    }

但是,当我尝试将另一个宠物添加到同一个主人ID时,它会转到下一个主人ID(在本例中,ID=7,而ID为6的主人不会发生任何事情。)本地主机:8080/宠物/插入)

{
    "name": "Roabson",
    "breed": "Pug",
    "birthDate": "2018-12-02T03:00:00.000+00:00",
    "owner":{
        "id":6
    }
}

我的主人(id=6)保持绝对不变,只有一个宠物,而我的下一个主人(id=7)接收这个宠物。

pjngdqdw

pjngdqdw1#

请尝试使用此示例。
在所有者类中:

@OneToMany(mappedBy = "owners", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Pets> pets = new HashSet<>();

宠物类:

@ManyToOne
@JoinColumn(name = "owners_id", referencedColumnName = "id")
private Owners owners;

最好不要用复数名词来表示类。

ha5z0ras

ha5z0ras2#

每次你用这种方式发送一个列表,你就重写它。你不添加一个项目,你就重写列表。如果你想添加,你就请求列表中已经存在的项目,添加一个新的,然后发送这个修改过的列表。

相关问题