@OneToMany 的错误使用返回空列表 [英] Wrong usage of @OneToMany returns empty list

查看:48
本文介绍了@OneToMany 的错误使用返回空列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型类:

拥有所有者字段的汽车:

Car which has owner field:

import lombok.Data;

import javax.persistence.*;

@Entity
@Data
public class Car {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String carBrand;
    private String modelOfCar;
    private String gearbox;
    private int yearProduction;
    private int mileage;
    private String status;
    @ManyToOne
    @JoinColumn(name = "owner_id")
    private Owner owner;
}

和拥有汽车领域的所有者

And Owner which has cars field

@Entity
@Data
public class Owner implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String nameOfOwner;
    private String surnameOfOwner;
    @OneToMany(mappedBy = "owner")
   
    private List<Car> cars = new ArrayList<>()
            
    public void addCarToOwner(Car car) {
        cars.add(car);
    }
}

我尝试将一些示例数据添加到此数据中的嵌入式 h2 DB:

I tried to add some sample data to embedded h2 DB within this data:

    Car car1 = new Car();
    car1.setCarBrand("asd");
    car1.setModelOfCar("asd");
    car1.setGearbox("asd");
    car1.setYearProduction(22018);
    car1.setMileage(321000);
    car1.setStatus("Available");

    Car car2 = new Car();
    car2.setCarBrand("asd");
    car2.setModelOfCar("asd");
    car2.setGearbox("asd");
    car2.setYearProduction(2011);
    car2.setMileage(6801020);
    car2.setStatus("Available");

  Owner owner1 = new Owner();
    owner1.setNameOfOwner("asd");
    owner1.setSurnameOfOwner("asd");
    owner1.addCarToOwner(car1);

    Owner owner2 = new Owner();
    owner2.setNameOfOwner("asd");
    owner2.setSurnameOfOwner("asd");
    owner2.addCarToOwner(car2);

  carRepository.save(car1);
    carRepository.save(car2);
    ownersRepository.save(owner1);
    ownersRepository.save(owner2);

当我使用 findAll() 方法检查所有者存储库的状态时,我得到所有者和他的汽车的空列表.当我为汽车存储库运行 findAll() 时,我的所有者为 null,因为我没有设置它,但是当所有者需要汽车并且汽车需要所有者时如何设置它?

When I check state of owner repository with findAll() method I get owner and empty list of his cars. When I run findAll() for cars repository I have null for owner, because I did not set it, but how can I set it when owner needs cars, and car need owner?

所有者存储库

汽车资料库

所有这些导致当我尝试获取具有给定 ID 的用户时,我会得到一个空的汽车列表.

All this causes that when I try to fetch user with given ID I get an empty list of cars for him.

推荐答案

看起来您正在创建 OwnerCar<之间的双向 OneToMany/代码>.在这种情况下,您需要同步关联的双方.

Looks like you're creating a bi-directional OneToMany between Owner and Car. In this case, you'll need to synchronize both sides of the association.

您的实用方法 addCarToOwner 可以很好地做到这一点:

Your utility method addCarToOwner could do this very well:

@Data
@Entity
public class Owner implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String nameOfOwner;
    private String surnameOfOwner;
    @OneToMany(mappedBy = "owner")
    private List<Car> cars = new ArrayList<>()
    
    public void addCarToOwner(Car car) {
        car.setOwner(this);
        this.cars.add(car);
    }
}

通过将 Owner 设置为 Car,您可以同步关联的双方.

By setting the Owner to the Car, you synchronize both sides of the association.

这篇关于@OneToMany 的错误使用返回空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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