我正在开发一个具有持久性的Java EE应用程序。
我的 Car 类有一些预订,而预订类扩展了引用类。
出于某种原因预订不是实体类。我的猜测是继承存在问题,但我似乎无法弄明白。
Car 看起来像这样:
@Entity
public class Car {
@Id
private int id;
@OneToOne(cascade=PERSIST, mappedBy="CarType")
private CarType type;
@OneToMany(cascade=REMOVE, mappedBy="Quote")
private Set<Reservation> reservations;
public Car() {}
.
.
.
public boolean equals(Object otherObject) {
...
}
public int hashCode() {
...
}
预订看起来像这样
@Entity
@Table(name = "Reservation")
public class Reservation extends Quote {
@Id
@GeneratedValue(strategy=AUTO)
@Column(name="reservationId")
private int reservationId;
private int carId;
public Reservation() {}
public Reservation(Quote quote, int carId) {
super(quote.getCarRenter(), quote.getStartDate(), quote.getEndDate(),
quote.getRentalCompany(), quote.getCarType(), quote.getRentalPrice());
this.carId = carId;
}
.
.
.
public boolean equals(Object otherObject) {
...
}
public int hashCode() {
...
}
}
引用看起来像这样:
@MappedSuperclass
public class Quote implements Serializable {
@Id
@GeneratedValue(strategy=AUTO)
@Column(name="quoteId")
private int quoteId;
@Temporal(DATE)
private Date startDate;
@Temporal(DATE)
private Date endDate;
private String carRenter;
private String rentalCompany;
private String carType;
private double rentalPrice;
public Quote() {}
public Quote(String carRenter, Date start, Date end, String rentalCompany, String carType, double rentalPrice) {
...
}
.
.
.
@Override
public int hashCode() {
...
}
@Override
public boolean equals(Object obj) {
...
}
}
为什么预订不是正确的实体类?
答案 0 :(得分:1)
实体预约必须删除@id,因为女儿从超类
继承它指定一个类,其映射信息应用于从其继承的实体。映射的超类没有为其定义单独的表。
答案 1 :(得分:0)
我通过以下方式解决了这个问题: