我想制作一张桌子,假设桌子的名字是 Car。它将有 3 列,brandId、typeId 和 sizeId。我希望所有的列都是主键。typeId 和 sizeId 是来自不同表的列。我已经尝试使用@IdClass 编写代码。但是,出现错误“未找到超类型”。
这是实体类:
@Entity
@IdClass(CarPk.class)
@Table(name = "CAR")
public class Car implements Serializable {
private static final long serialVersionUID = -1576946068763487642L;
public Car() {
}
public Car(String brandId, TypeId typeId, SizeId sizeId) {
this.brandId = brandId;
this.typeId = typeId;
this.sizeId = sizeId;
}
@Id
@Column(name = "BRAND_ID", nullable = false, length = 20)
private String brandId;
@Id
@ManyToOne
@JoinColumn(name = "TYPE_ID", nullable = false)
private TypeId typeId;
@Id
@ManyToOne
@JoinColumn(name = "SIZE_ID", nullable = false)
private SizeId sizeId;
public String getBrandId() {
return brandId;
}
public void setBrandId(String brandId) {
this.brandId= brandId;
}
public TypeId getTypeId() {
return typeId;
}
public void setTypeId (TypeId typeId) {
this.typeId= typeId;
}
public SizeId getSizeId() {
return sizeId;
}
public void setSizeId (SizeId sizeId) {
this.sizeId= sizeId;
}
}
这是 CarPk 类:
public class CarPk implements Serializable {
private static final long serialVersionUID = -1576946068763487642L;
public CarPk() {
}
public CarPk(String brandId, TypeId typeId, SizeId sizeId) {
this.brandId = brandId;
this.typeId = typeId;
this.sizeId = sizeId;
}
private String brandId;
private TypeId typeId;
private SizeId sizeId;
public String getBrandId() {
return brandId;
}
public void setBrandId(String brandId) {
this.brandId= brandId;
}
public TypeId getTypeId() {
return typeId;
}
public void setTypeId (TypeId typeId) {
this.typeId= typeId;
}
public SizeId getSizeId() {
return sizeId;
}
public void setSizeId (SizeId sizeId) {
this.sizeId= sizeId;
}
}
这段代码有什么问题?还是非常感谢