最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - How to configure a N:1:1:N SQL relation on SpringBoot while also using DTOs? - Stack Overflow

programmeradmin0浏览0评论

I made this minimal example to help visualize the relation I want to implement:

AFAIK in Spring Boot I need to configure:

On Car:

@ManyToMany
@JoinTable(
        name = "cars_tags",
        joinColumns = @JoinColumn(name = "car_id"),
        inverseJoinColumns = @JoinColumn(name = "tag_id")
)
private Set<Tags> tags= new HashSet<>();

And on Tag:

@ManyToMany(mappedBy = "tags")
private Set<Car> cars = new HashSet<>();

However, I'm also utilizing DTOs, so when creating a car, the REST body needs to have a CarRequestDTO:

private String model
private String year

private Set<String> tagIds;

Finally, I'd save on the @Service class, using the Car repository, with:

    // validating DTO
    validateCar(carRequestDTO);

    // convert CarRequestDTO to Car and copying properties
    Car car = new Car();

    BeanUtils.copyProperties(carRequestDTO, car);

    // save
    carRepository.save(car);

However, when saving, the table cars_tags retains no data at all. I'd like to know what I could possibly doing wrong (or not doing at all).

Thank you for the help.

发布评论

评论列表(0)

  1. 暂无评论