简体   繁体   English

使用 spring boot, JPA 通过引用关联实体

[英]relate entities by reference using spring boot, JPA

Say we have 2 classes Driver and Car with the Driver having a many-to-one relationship with the Car as follows.假设我们有 2 个类 Driver 和 Car,其中 Driver 与 Car 具有多对一的关系,如下所示。

@Entity
@Table(name = "driver")
public class Driver {
    @Id @GeneratedValue
    private Long id;

    @ManyToOne
    private Car car;

    ...

    // getter setter ignored for brevity
}

Is there a way to set the value of car via post request for example by referencing car by its id by just JPA/Hibernate annotations ?有没有办法通过发布请求来设置car的值,例如通过JPA/Hibernate 注释通过其id引用car I'm still sort of new to Spring boot, so I was actually thinking of creating a new attribute Long carId and then apply @JsonIgnore to car , according to https://stackoverflow.com/a/42633336/9324939 .根据https://stackoverflow.com/a/42633336/9324939 ,我对 Spring boot 还是有点Long carId ,所以我实际上是在考虑创建一个新属性Long carId然后将@JsonIgnore应用到car Or is there any other suggestion or approach to get what I'm trying to achieve?或者有没有其他建议或方法来实现我想要实现的目标?

PS: In the database, they are already connected by reference. PS:在数据库中,它们已经通过引用连接了。

-- in postgres

...

driver_id    BIGINTEGER    REFERENCES  car (id)

...

please take a look here for a sample project I made to address this:请在此处查看我为解决此问题而制作的示例项目:

https://github.com/Fermi-4/StackOverflow---JPA-Relationshipshttps://github.com/Fermi-4/StackOverflow---JPA-Relationships

Once started locally, use Postman to set the car to a driver:在本地启动后,使用 Postman 将汽车设置为驱动程序:

http://localhost:9090/api/assigncar?driverId=1&carId=1 http://localhost:9090/api/assigncar?driverId=1&carId=1

Driver Entity - using Lombok驱动程序实体 - 使用 Lombok

@Entity
@Table(name = "driver")
@Data
public class Driver {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long driverId;

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Car car;
}

Car Entity - using Lombok and @JsonIgnore to prevent infinite recursion汽车实体 - 使用 Lombok 和 @JsonIgnore 来防止无限递归

@Entity
@Table(name = "car")
@Data
public class Car {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long carId;

    @JsonIgnore
    @OneToMany
    private List<Driver> drivers = new ArrayList<Driver>();

}

Repositories存储库

public interface CarRepository extends JpaRepository<Car, Long> {    }
public interface DriverRepository extends JpaRepository<Driver, Long> {    }

Controller Class控制器类

@RequestMapping("/api")
@RestController
public class DriverController {

    @Autowired
    CarRepository _carRepo;

    @Autowired
    DriverRepository _driverRepo;

    @PostMapping("/assigncar")
    public Driver assignCarToDriver(@RequestParam Long driverId,@RequestParam Long carId) {
         Car car = _carRepo.findById(carId).get();
         Driver driver = _driverRepo.findById(driverId).get();
         driver.setCar(car);
         return _driverRepo.save(driver);
    }

}

当您通过 post request 添加新司机时,您可以在您的 json 对象中分配一辆新车或一辆现有汽车(您可以尝试在您的 @ManyToOne 中添加 cascadeType.ALL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM