2015-12-03 18 views
1

我正在敲我的頭,因爲以下問題:休眠:org.hibernate.WrongClassException

我有2個實體clases繼承一個基類。基類只存儲創建/更新日期。

基類:

@Entity 
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 
public abstract class BaseEntity { 

@Id 
@GeneratedValue(strategy = GenerationType.TABLE) 
@Column(name = "id") 
private int id; 

@Temporal(TemporalType.TIMESTAMP) 
@Column(name = "CREATED", nullable = true) 
private Date created; 

@PrePersist 
protected void onCreate() { 
    created = new Date(); 
} 

... 

子類1:

@Entity 
@Table(name = "hotel") 
@XmlRootElement 
public class Hotel extends BaseEntity implements Serializable 
... 
@OneToMany(fetch = FetchType.EAGER, mappedBy = "hotel", cascade = CascadeType.ALL, orphanRemoval = true) 
private Set<HotelManager> hotelManagers = new HashSet<HotelManager>(); 
... 

子類2:

@Entity 
@Table(name = "hotel_manager") 
public class HotelManager extends BaseEntity implements Serializable { 

... 
@ManyToOne(fetch = FetchType.EAGER) 
@JoinColumn(name = "hotel_id", nullable = false) 
private Hotel hotel; 
.... 

每當我嘗試加載酒店方i得到以下錯誤:

org.hibernate.WrongClassException:具有id:7的對象不是指定的子類:de.hop.entity.HotelManager(加載的對象屬於錯誤的類class de.hop.entity.Hotel)

附註:偶然酒店行ID(唯一ID),以及酒店管理者行ID都是'7'在兩個分表

任何想法?

+1

請在加載實體的位置添加代碼,並且爲什麼要使用@inheraticance註釋。你可以添加@ MappedSuperclass到你的基類。 –

+0

嘗試將您的抓取類型設置爲懶惰,以便在酒店中設置酒店經理。問題最可能的是試圖加載酒店經理熱切關注的酒店。 –

+0

@si mo解決了我的問題。謝謝! –

回答

0

您可以將@ MappedSuperclass而不是@Inheritance添加到您的基類。

+0

此答案具有誤導性,因爲在執行此操作時:「org.hibernate.AnnotationException實體不能同時使用@Entity和@MappedSuperclass進行註釋」。所以它應該在您的答案中讀取「而不是@Entity」?請檢查/澄清。 – MWiesner