2014-03-24 75 views
0

首先,我想說我已經閱讀了數十篇有關繼承映射或多態獲取與休眠沒有找到解決我的問題的文章。 雖然情況很簡單。HQL無法讀取屬性,這是子類的一個實例

執行以下代碼時: List meals = sessionFactory.getCurrentSession() .createQuery(「select m from Meal m」)。list(); 我得到的錯誤如下: org.hibernate.WrongClassException:與ID對象:1是指定的子類的沒有:com.myPack.Fruit(鑑別:橙色)

其實,我看不懂抽象類這是在桔子或蘋果

吃飯子類的水果含有水果可以是桔子或蘋果

@Entity 
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) 
@Table(name = "fruit") 
public abstract class Fruit { 

    @Id 
    @GeneratedValue 
    @Column(name = "id", unique = true) 
    private Integer id; 
} 

@Entity(name = "Orange") //this is the discriminatorValue stored in dtype column 
public class Orange extends Fruit { 
} 

@Entity(name = "Apple") //this is the discriminatorValue stored in dtype column 
public class Orange extends Fruit { 
} 

@Entity 
@Table(name = "meal") 
public class Meal { 
    ... 

    @ManyToOne 
    @JoinColumn(name = "fruit_id", nullable = false) 
    private Fruit fruit = null; 
} 

的,所以我不應該與任何代理問題的獲取更是不可以偷懶。 我不明白爲什麼冬眠不能夠找出這樣的水果是橙色或蘋果,因爲水果表包含在DTYPE列鑑別器橙色或蘋果

我將非常感謝解決這個問題。 (對不起,我的模糊英語)

。 。

經過進一步的研究,我發現@DiscriminatorOptions(force = true)可以解決我的麻煩,但最後......無奈。 我所學到的是,即使hibernate具有找到具體類的所有信息,在某些情況下也需要註釋@DiscriminatorOptions(force = true)。 無論如何,我想告訴hibernate:「嘿,休眠不要試圖instanciate抽象水果類,因爲它是抽象的。嘿休眠嘗試依賴@DiscriminatorValue實體設置」..但冬眠是聾子:(

回答

0

更換這一點,並嘗試

List meals = sessionFactory.getCurrentSession().createQuery("from Meal").list(); 
+0

在4.1.0.Final hibernate版本中,更改fom createQuery(「從m中選擇m」)到createQuery(「from Meal」)沒有影響。 但移動到4.2.4或4.3.4版本的行爲是不同的,但在這兩種情況下,問題依然存在: – user3455706

+0

createQuery(「來自Meal」)暗示異常 createQuery(「從m餐中選擇m」) Meal的屬性水果爲空。畢竟,它在功能上是同樣的問題。 – user3455706

0

我一直在使用Spring 4.0.2和4.3.4 Hibernate的經歷完全一樣的問題,我覺得有東西從我的項目的依賴或可能只是一個錯誤失蹤在休眠。

我建議你回滾你的Hibernate版本,最後一個工作版本我哈d是4.2.4,我正在恢復過程中。

+0

改變fom 4.1.0。最終的hibernate版本到4.2.4或4.3.4版本的行爲是不同的,但在這兩種情況下問題依然存在:4.1.0中的 。最終的createQuery(「從m選擇m」意味着4.2.4或4.3.4中的異常 createQuery(「從m餐中選擇m」)不會引發異常,但Meal的屬性結果爲null。畢竟,它在功能上是同樣的問題。 – user3455706