Hibernate JPA-一个实体如何引用多人关系中涉及的其他两个实体?

Hibernate JPA-一个实体如何引用多人关系中涉及的其他两个实体?,hibernate,jpa,jpa-2.1,Hibernate,Jpa,Jpa 2.1,我有以下表格: 在我的域模型中,我有一个Car实体: @Entity @Data public class Car { @Id private Long id; @ManyToMany @JoinTable(name="CarClassType", joinColumns= @JoinColumn(name="carId"), inverseJoinColumns = @JoinColu

我有以下表格:

在我的域模型中,我有一个
Car
实体:

@Entity
@Data
public class Car {

    @Id
    private Long id;

    @ManyToMany
    @JoinTable(name="CarClassType",
        joinColumns= @JoinColumn(name="carId"),
        inverseJoinColumns = @JoinColumn(name="classTypeId"))
    private List<CarType> carTypes;
    
    private String model;
}

此处的建模不需要
@manytomy
,因为您可以将
CarClassType
建模为实体。由于
CarClassType
表有一个额外的列
id
,因此实际上您也不能以这种方式对其建模。如果您想要这样做,您必须同时使用两个FK作为主键,并删除
id
列,我建议您无论如何都要这样做

@Entity
@Data
public class Car {

    @Id
    private Long id;

    @OneToMany(mappedBy = "car")
    private Set<CarClassType> carTypes;
    
    private String model;
}
@Entity
@Data
public class CarClassType {

    @EmbeddedId
    private CarClassTypeId id;

    @ManyToOne(fetch = LAZY)
    @JoinColumn(name = "carId", insertable = false, updatable = false)
    private Car car;
    
    @ManyToOne(fetch = LAZY)
    @JoinColumn(name = "classTypeId", insertable = false, updatable = false)
    private CarType classType;

    @OneToMany(mappedBy = "carClassType")
    private Set<CarClassTypeSale> sales = new HashSet<>();
}
@Embeddable
@Data
public class CarClassTypeId {

    @Column(name = "carId")
    private Integer carId;
    
    @Column(name = "classTypeId")
    private Integer classTypeId;
}
@Entity
@Data
public class CarClassTypeSale {

    @Id
    private Long id;

    @ManyToOne
    private CarClassType carClassType;

    private LocalDate soldDate;


}
@实体
@资料
公车{
@身份证
私人长id;
@OneToMany(mappedBy=“汽车”)
私有集合类型;
私有字符串模型;
}
@实体
@资料
公共类CarClassType{
@嵌入ID
私有CarClassTypeId;
@manytone(fetch=LAZY)
@JoinColumn(name=“carId”,insertable=false,updateable=false)
私家车;
@manytone(fetch=LAZY)
@JoinColumn(name=“classTypeId”,可插入=false,可更新=false)
私有cartypeclasstype;
@OneToMany(mappedBy=“carClassType”)
private Set sales=new HashSet();
}
@可嵌入
@资料
公共类CarClassTypeId{
@列(name=“carId”)
私有整数carId;
@列(name=“classTypeId”)
私有整数classTypeId;
}
@实体
@资料
公共类汽车销售{
@身份证
私人长id;
@许多酮
私有CarClassType CarClassType;
私有LocalDate soldDate;
}

不确定
CarClassTypeSale
是否适合此图片。您的图表并没有真正告诉我该课程与其他课程的关系。

谢谢您的回答
CarClassTypeSale
捕获属于特定类别类型的汽车的销售情况。因此,
CarClassTypeSale
指的是一个
CarClassType
CarClassType
中的记录指的是一个
Car
和一个
ClassType
。因此,
CarClassTypeSale
间接指一辆
Car
和一辆
ClassType
。也许,我误解了你关于它与其他人的关系的说明,但我确实在图表中看到了这种关系。另外,你能告诉我为什么你更喜欢去掉
CarClassType
id
列吗?我同意如果我们让FKs成为复合PK,就不需要它,但是
id
消除了嵌入的需要。我更新了模型。你写了一些关于多对多关联的东西,但是如果你引入了代理
id
,这就不是多对多关联了。除此之外,您不需要对这种特殊情况建模,因为多对多,而且通常情况下,一个人并不需要多对多关联。我贴的模型应该适合你的情况。谢谢。最后,我想看看是否有可能没有
CarClassType
实体。我可以从
CarClassType
表中删除
id
列。正如你所指出的,这是没有必要的。但我认为,为了定义
CarClassTypeSale
实体和两个实体
Car
ClassType
之间的关系,有必要有一个
CarClassType
实体,你同意吗?你可以用任何你喜欢的方式建模,但是您应该如何对其建模应该由需求驱动,因为这在语义上会产生差异。我不知道您的实际需求,但在这个特定的示例中,我只想删除
CarClassType
@Entity
@Data
public class Car {

    @Id
    private Long id;

    @OneToMany(mappedBy = "car")
    private Set<CarClassType> carTypes;
    
    private String model;
}
@Entity
@Data
public class CarClassType {

    @EmbeddedId
    private CarClassTypeId id;

    @ManyToOne(fetch = LAZY)
    @JoinColumn(name = "carId", insertable = false, updatable = false)
    private Car car;
    
    @ManyToOne(fetch = LAZY)
    @JoinColumn(name = "classTypeId", insertable = false, updatable = false)
    private CarType classType;

    @OneToMany(mappedBy = "carClassType")
    private Set<CarClassTypeSale> sales = new HashSet<>();
}
@Embeddable
@Data
public class CarClassTypeId {

    @Column(name = "carId")
    private Integer carId;
    
    @Column(name = "classTypeId")
    private Integer classTypeId;
}
@Entity
@Data
public class CarClassTypeSale {

    @Id
    private Long id;

    @ManyToOne
    private CarClassType carClassType;

    private LocalDate soldDate;


}