2015-11-11 21 views
1

我正在開發具有持久性的Java EE應用程序。Java EE - [class <ClassName]使用非實體[類<ClassName>]作爲關係屬性中的目標實體

汽車類有幾個預訂預訂類擴展報價類。

由於某種原因保留不是實體類。我的猜測是繼承有問題,但我似乎無法弄清楚。

汽車看起來是這樣的:

@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

我做下面的事情解決了這個問題:

  • 我刪除的ID從預訂因爲它是從報價
  • 我手動添加預訂persistence.xml中繼承了作爲一個實體
1

,因爲女兒從超類

繼承它的實體預訂必須刪除@Id指定一個類,其映射信息被應用到繼承它的實體。映射的超類沒有爲其定義單獨的表。

+0

很好的答案。這與手動將預留添加到我的Persistence.xml中解決了問題。 –

相關問題