Java 如何用Spring数据JDBC建模一对一关系?

Java 如何用Spring数据JDBC建模一对一关系?,java,postgresql,spring-boot,spring-data-jdbc,Java,Postgresql,Spring Boot,Spring Data Jdbc,我想使用Spring数据JDBC和PostgreSQL对一对一关系进行建模,但在正确设置根聚合时遇到了问题 有以下情况:, 每个发动机都是唯一的,car有唯一的列engine\u id,这是engine.id的外键,卡车也一样。所以car和truck应该是根集合,所以当删除car或truck时,引擎表中引用的行也应该删除 根据我对 如果多个聚合引用同一个实体,则该实体不能是引用它的聚合的一部分,因为它只能是一个聚合的一部分 因此,问题是: 由于上面的解释,是否可以解决问题,以便通过对汽车和卡车

我想使用Spring数据JDBC和PostgreSQL对一对一关系进行建模,但在正确设置根聚合时遇到了问题

有以下情况:,
每个发动机都是唯一的,
car
有唯一的列
engine\u id
,这是
engine.id
的外键,卡车也一样。所以car和truck应该是根集合,所以当删除car或truck时,引擎表中引用的行也应该删除

根据我对

如果多个聚合引用同一个实体,则该实体不能是引用它的聚合的一部分,因为它只能是一个聚合的一部分

因此,问题是:

  • 由于上面的解释,是否可以解决问题,以便通过对
    汽车
    卡车
    执行积垢操作,更改也会反映到
    发动机
  • 使用Spring数据JDBC在java中实现这种关系的最佳方法是什么
这是我的观点,它不起作用,但应该澄清我试图实现的目标

java

package com.example.dao.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

import java.util.UUID;

@Table("car")
public class Car implements Persistable<UUID> {

    @Id
    private UUID id;

    String brand;

    String model;

    @Column("engine_id")
    Engine engine;

    public void setId(UUID id) {
        this.id = id;
    }

    @Override
    public UUID getId() {
        return id;
    }

    @Override
    public boolean isNew() {
        return id == null;
    }
}
package com.backend.dao.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;

import java.util.Objects;
import java.util.UUID;

public class Car implements Persistable<UUID> {

    @Id
    private UUID id;

    private String brand;

    private String model;

    public void setId(UUID id) {
        this.id = id;
    }

    @Override
    public UUID getId() {
        return id;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    @Override
    public boolean isNew() {
        return id == null;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Car)) {
            return false;
        }
        Car car = (Car) o;
        return Objects.equals(id, car.id) &&
            Objects.equals(brand, car.brand) &&
            Objects.equals(model, car.model);
    }

    @Override
    public int hashCode() {

        return Objects.hash(id, brand, model);
    }
}
package com.example.dao.model;
导入org.springframework.data.annotation.Id;
导入org.springframework.data.domain.Persistable;
导入org.springframework.data.relational.core.mapping.Column;
导入org.springframework.data.relational.core.mapping.Table;
导入java.util.UUID;
@表(“汽车”)
公共类Car实现持久化{
@身份证
私有UUID;
串品牌;
弦模型;
@列(“引擎id”)
发动机;
公共无效设置id(UUID id){
this.id=id;
}
@凌驾
公共UUID getId(){
返回id;
}
@凌驾
public boolean isNew(){
返回id==null;
}
}
Engine.java

package com.example.dao.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;
import org.springframework.data.relational.core.mapping.Table;

import java.time.LocalDateTime;
import java.util.UUID;

@Table("engine")
public class Engine implements Persistable<UUID> {

    @Id
    private UUID id;

    String name;

    LocalDateTime dateCreated;

    String type;

    public void setId(UUID id) {
        this.id = id;
    }

    @Override
    public UUID getId() {
        return id;
    }

    @Override
    public boolean isNew() {
        return id == null;
    }
}
package com.backend.dao.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.UUID;

@Table("engine")
public class Engine implements Persistable<UUID> {

    @Id
    private UUID id;

    private String name;

    private LocalDateTime dateCreated;

    private String type;

    @Column("engine_id")
    private Car car;

    @Column("engine_id")
    private Truck truck;

    public void setId(UUID id) {
        this.id = id;
    }

    @Override
    public UUID getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public LocalDateTime getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(LocalDateTime dateCreated) {
        this.dateCreated = dateCreated;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public boolean isNew() {
        return id == null;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Engine)) {
            return false;
        }
        Engine engine = (Engine) o;
        return Objects.equals(id, engine.id) &&
            Objects.equals(name, engine.name) &&
            Objects.equals(dateCreated, engine.dateCreated) &&
            Objects.equals(type, engine.type);
    }

    @Override
    public int hashCode() {

        return Objects.hash(id, name, dateCreated, type);
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public Truck getTruck() {
        return truck;
    }

    public void setTruck(Truck truck) {
        this.truck = truck;
    }
}
package com.example.dao.model;
导入org.springframework.data.annotation.Id;
导入org.springframework.data.domain.Persistable;
导入org.springframework.data.relational.core.mapping.Table;
导入java.time.LocalDateTime;
导入java.util.UUID;
@表(“发动机”)
公共类引擎实现持久化{
@身份证
私有UUID;
字符串名;
LocalDateTime-dateCreated;
字符串类型;
公共无效设置id(UUID id){
this.id=id;
}
@凌驾
公共UUID getId(){
返回id;
}
@凌驾
public boolean isNew(){
返回id==null;
}
}
Truck.java

package com.example.dao.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

import java.util.UUID;

@Table("truck")
public class Truck implements Persistable<UUID> {

    @Id
    private UUID id;

    String brand;

    String model;

    Integer cargoMaxWeight;

    String truckType;

    @Column("engine_id")
    Engine engine;

    public void setId(UUID id) {
        this.id = id;
    }

    @Override
    public UUID getId() {
        return id;
    }

    @Override
    public boolean isNew() {
        return id == null;
    }
}
package com.backend.dao.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;
import org.springframework.data.relational.core.mapping.Table;

import java.util.Objects;
import java.util.UUID;

@Table("truck")
public class Truck implements Persistable<UUID> {

    @Id
    private UUID id;

    private String brand;

    private String model;

    private Integer cargoMaxWeight;

    private String truckType;

    public void setId(UUID id) {
        this.id = id;
    }

    @Override
    public UUID getId() {
        return id;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public Integer getCargoMaxWeight() {
        return cargoMaxWeight;
    }

    public void setCargoMaxWeight(Integer cargoMaxWeight) {
        this.cargoMaxWeight = cargoMaxWeight;
    }

    public String getTruckType() {
        return truckType;
    }

    public void setTruckType(String truckType) {
        this.truckType = truckType;
    }

    @Override
    public boolean isNew() {
        return id == null;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Truck)) {
            return false;
        }
        Truck truck = (Truck) o;
        return Objects.equals(id, truck.id) &&
            Objects.equals(brand, truck.brand) &&
            Objects.equals(model, truck.model) &&
            Objects.equals(cargoMaxWeight, truck.cargoMaxWeight) &&
            Objects.equals(truckType, truck.truckType);
    }

    @Override
    public int hashCode() {

        return Objects.hash(id, brand, model, cargoMaxWeight, truckType);
    }
}
package com.example.dao.model;
导入org.springframework.data.annotation.Id;
导入org.springframework.data.domain.Persistable;
导入org.springframework.data.relational.core.mapping.Column;
导入org.springframework.data.relational.core.mapping.Table;
导入java.util.UUID;
@表(“卡车”)
公共类卡车实现持久化{
@身份证
私有UUID;
串品牌;
弦模型;
整数最大权重;
字符串结构类型;
@列(“引擎id”)
发动机;
公共无效设置id(UUID id){
this.id=id;
}
@凌驾
公共UUID getId(){
返回id;
}
@凌驾
public boolean isNew(){
返回id==null;
}
}

设法找到了解决方案,问题是当数据库中的模型不同时,我无法从
引擎中引用
汽车
卡车

java

package com.example.dao.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

import java.util.UUID;

@Table("car")
public class Car implements Persistable<UUID> {

    @Id
    private UUID id;

    String brand;

    String model;

    @Column("engine_id")
    Engine engine;

    public void setId(UUID id) {
        this.id = id;
    }

    @Override
    public UUID getId() {
        return id;
    }

    @Override
    public boolean isNew() {
        return id == null;
    }
}
package com.backend.dao.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;

import java.util.Objects;
import java.util.UUID;

public class Car implements Persistable<UUID> {

    @Id
    private UUID id;

    private String brand;

    private String model;

    public void setId(UUID id) {
        this.id = id;
    }

    @Override
    public UUID getId() {
        return id;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    @Override
    public boolean isNew() {
        return id == null;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Car)) {
            return false;
        }
        Car car = (Car) o;
        return Objects.equals(id, car.id) &&
            Objects.equals(brand, car.brand) &&
            Objects.equals(model, car.model);
    }

    @Override
    public int hashCode() {

        return Objects.hash(id, brand, model);
    }
}
package com.backend.dao.model;
导入org.springframework.data.annotation.Id;
导入org.springframework.data.domain.Persistable;
导入java.util.Objects;
导入java.util.UUID;
公共类Car实现持久化{
@身份证
私有UUID;
自有品牌;
私有字符串模型;
公共无效设置id(UUID id){
this.id=id;
}
@凌驾
公共UUID getId(){
返回id;
}
公共字符串getBrand(){
回归品牌;
}
公共品牌(字符串品牌){
这个品牌=品牌;
}
公共字符串getModel(){
收益模型;
}
公共void集合模型(字符串模型){
this.model=模型;
}
@凌驾
public boolean isNew(){
返回id==null;
}
@凌驾
公共布尔等于(对象o){
if(this==o){
返回true;
}
如果(!(车辆实例)){
返回false;
}
汽车=(汽车)o;
返回Objects.equals(id,car.id)&&
Objects.equals(品牌、汽车、品牌)&&
Objects.equals(model,car.model);
}
@凌驾
公共int hashCode(){
返回Objects.hash(id、brand、model);
}
}
Truck.java

package com.example.dao.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

import java.util.UUID;

@Table("truck")
public class Truck implements Persistable<UUID> {

    @Id
    private UUID id;

    String brand;

    String model;

    Integer cargoMaxWeight;

    String truckType;

    @Column("engine_id")
    Engine engine;

    public void setId(UUID id) {
        this.id = id;
    }

    @Override
    public UUID getId() {
        return id;
    }

    @Override
    public boolean isNew() {
        return id == null;
    }
}
package com.backend.dao.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;
import org.springframework.data.relational.core.mapping.Table;

import java.util.Objects;
import java.util.UUID;

@Table("truck")
public class Truck implements Persistable<UUID> {

    @Id
    private UUID id;

    private String brand;

    private String model;

    private Integer cargoMaxWeight;

    private String truckType;

    public void setId(UUID id) {
        this.id = id;
    }

    @Override
    public UUID getId() {
        return id;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public Integer getCargoMaxWeight() {
        return cargoMaxWeight;
    }

    public void setCargoMaxWeight(Integer cargoMaxWeight) {
        this.cargoMaxWeight = cargoMaxWeight;
    }

    public String getTruckType() {
        return truckType;
    }

    public void setTruckType(String truckType) {
        this.truckType = truckType;
    }

    @Override
    public boolean isNew() {
        return id == null;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Truck)) {
            return false;
        }
        Truck truck = (Truck) o;
        return Objects.equals(id, truck.id) &&
            Objects.equals(brand, truck.brand) &&
            Objects.equals(model, truck.model) &&
            Objects.equals(cargoMaxWeight, truck.cargoMaxWeight) &&
            Objects.equals(truckType, truck.truckType);
    }

    @Override
    public int hashCode() {

        return Objects.hash(id, brand, model, cargoMaxWeight, truckType);
    }
}
package com.backend.dao.model;
导入org.springframework.data.annotation.Id;
导入org.springframework.data.domain.Persistable;
导入org.springframework.data.relational.core.mapping.Table;
导入java.util.Objects;
导入java.util.UUID;
@表(“卡车”)
公共类卡车实现持久化{
@身份证
私有UUID;
自有品牌;
私有字符串模型;
私有整数最大权重;
私有字符串结构类型;
公共无效设置id(UUID id){
this.id=id;
}
@凌驾
公共UUID getId(){
返回id;
}
公共字符串getBrand(){
回归品牌;
}
公共品牌(字符串品牌){
这个品牌=品牌;
}
公共字符串getModel(){
收益模型;
}
公共void集合模型(字符串模型){
this.model=模型;
}
公共整数getCargoMaxWeight(){
返回货物最大重量;
}
public void setCargoMaxWeight(整数cargoMaxWeight){
this.cargoMaxWeight=cargoMaxWeight;
}
公共字符串getRuckType(){
返回卡车类型;
}
公共void setTruckType(字符串truckType){
this.truckType=truckType;
}
@凌驾
public boolean isNew(){
返回id==nu