首先,我想說我已經閱讀了數十篇有關繼承映射或多態獲取與休眠沒有找到解決我的問題的文章。 雖然情況很簡單。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實體設置」..但冬眠是聾子:(
在4.1.0.Final hibernate版本中,更改fom createQuery(「從m中選擇m」)到createQuery(「from Meal」)沒有影響。 但移動到4.2.4或4.3.4版本的行爲是不同的,但在這兩種情況下,問題依然存在: – user3455706
createQuery(「來自Meal」)暗示異常 createQuery(「從m餐中選擇m」) Meal的屬性水果爲空。畢竟,它在功能上是同樣的問題。 – user3455706