我有即時試圖實現一個類,(類似於「與Hibernate Java持久性」 6.3.3中的例子)分配一個@CollectionId並@ElementCollection
public class Property
{
...
@ElementCollection(fetch=FetchType.EAGER)
@CollectionId(
[email protected](name="Property_image_id"),
[email protected](type="long"),
generator="native"
)
private Collection<FileAttachment> images = new ArrayList<FileAttachment>();
...
}
的單元測試失敗拋以下例外:
java.lang.ClassCastException: org.hibernate.id.IdentifierGeneratorHelper$2
cannot be cast to java.lang.Long
我不太確定'發電機'的最佳值,我認爲這會影響結果。
此外,當我得到這個工作,可以FileAttachment對象訪問Property_image_id?你怎麼把它分配給一個屬性,因爲它在Property類中定義了它?
我想要的是爲Property_images表有一個組合鍵[Property_id-Image_index],其中Image_index從1開始爲每個新的Property_id,但我不知道如何使用@ElementCollection和@CollectionId實現這個發電機。也許我必須將FileAttachment作爲@Entity而不是@Embeddable,但是id不是它在Property類中唯一使用的。
乾杯! NFV
貌似嵌入ID依賴於實現呢。無論如何,也許@OrderColumn將是什麼即時通訊尋找,虐待嘗試!順便說一下,你知道從hibernate加載的子對象是否能夠訪問order列嗎? – nfvindaloo
我從來沒有試圖映射'OrderColumn'(因爲它只是父列表中元素的索引,可以通過[List.indexOf](http://docs.oracle.com/javase/) 6/docs/api/java/util/List.html#indexOf(java.lang.Object))。無論如何,你可以嘗試'@Column(name =「IMAGE_INDEX」,insertable = false,updatable = false)',if它工作後的結果 –
OrderColumn工作得很好,謝謝!通過列的訪問圖像索引,因爲你建議沒有工作。我認爲生病必須忘記註釋,並回落在老式的代碼(我知道令人震驚),和從子對象內部做這樣的事情: return parent.images.indexOf(this); – nfvindaloo