我有以下情况(为了使问题更清楚:其他字段/获取者/设置者被省略):
@Entity
@Table(name = "car")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Car {
}
@Entity
public class PassengerCar extends Car {
@OneToOne(mappedBy = "car", cascade = CascadeType.ALL, orphanRemoval = true)
private SpireTire spareTire;
}
@Entity
public class Truck extends Car {
@OneToMany(mappedBy = "car", cascade = CascadeType.ALL, orphanRemoval = true)
private List<SpireTire> spareTires = new LinkedList<>();
}
@Entity
public class SpireTire {
@ManyToOne
@JoinColumn(name = "id_car")
private Car car;
}
以下假设:
1
备用轮胎OneToOne
有OneToMany
或SpireTire
关系的汽车实现方式目标是根据上述假设在“汽车”和“ SpireTire”之间建立正确的关系
我看到两种实现方式:
SpireTire
实体对基本实体/表foreign key
具有Car
,并且每个汽车实现定义自己的关系。我对此有何看法:
SpireTire
行以控制每个汽车实施的条目数时,需要进行验证。所以为汽车SpireTire
添加新的PassengerCar
要求在每次插入之前进行检查,并在数据库中查找给定汽车的行是否已存在,如我们声明的{{1 }},但处于SpireTire
-ManyToOne
关系。PassengerCar
中的一个OneToOne
foreign key
分别有一个SpireTire
。就像这样:foreign key
以这种方式:
implementation
个汽车实现,其中@Entity
@Table(name = "car")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Car {
}
@Entity
public class PassengerCar extends Car {
@OneToOne(mappedBy = "passengerCar", cascade = CascadeType.ALL, orphanRemoval = true)
private SpireTire spareTire;
}
@Entity
public class Truck extends Car {
@OneToMany(mappedBy = "truck", cascade = CascadeType.ALL, orphanRemoval = true)
private List<SpireTire> spareTires = new LinkedList<>();
}
@Entity
public class SpireTire {
@OneToOne
@JoinColumn(name = "id_passenger_car")
private PassengerCar passengerCar;
@ManyToOne
@JoinColumn(name = "id_truck")
private Truck truck;
}
个是10
,而只是9
为@OneToMany
,则我必须定义1
个不同的{ {1}}个。问题:
我应该像在@OneToOne
点那样,在10
点那样实现还是完全不同?