問題描述
有人可以向我解釋一下 Any 相關的注釋(@Any
、@AnyMetaDef
、@AnyMetaDefs
和 @ManyToAny
) 在實踐中工作.我很難找到任何關于這些的有用文檔(僅 JavaDoc 不是很有幫助).
Could someone explain to me how Any-related annotations (@Any
, @AnyMetaDef
, @AnyMetaDefs
and @ManyToAny
) work in practice. I have a hard time finding any useful documentation (JavaDoc alone isn't very helpful) about these.
到目前為止,我已經收集到它們以某種方式啟用對抽象類和擴展類的引用.如果是這種情況,為什么沒有 @OneToAny
注釋?這個any"是指一個any"還是多個any"?
I have thus far gathered that they somehow enable referencing to abstract and extended classes. If this is the case, why is there not an @OneToAny
annotation? And is this 'any' referring to a single 'any', or multiple 'any'?
非常感謝一個簡短、實用且說明性的示例(不必編譯).
A short, practical and illustrating example would be very much appreciated (doesn't have to compile).
盡管我愿意接受作為答案的回復并在適當的時候給予信任,但我發現 Smink 和 Sakana 的答案都提供了豐富的信息.因為我不能接受多個回復作為答案,所以很遺憾,我將兩者都標記為答案.
as much as I would like to accept replies as answers and give credit where due, I found both Smink's and Sakana's answers informative. Because I can't accept several replies as the answer, I will unfortunately mark neither as the answer.
推薦答案
希望這篇文章 為主題帶來一些亮點:
Hope this article brings some light to the subject:
有時我們需要映射一個關聯屬性不同沒有屬性的實體類型共同祖先實體 - 如此簡單多態關聯不起作用工作.
Sometimes we need to map an association property to different types of entities that don't have a common ancestor entity - so a plain polymorphic association doesn't do the work.
例如,讓我們假設管理媒體庫的三個不同應用程序 - 第一個應用程序管理圖書借閱,第二個應用程序管理 DVD,第三個應用程序管理 VHS.這些應用程序沒有任何共同點.現在我們要開發一個新的應用程序來管理所有三種媒體類型并重用現有的 Book、DVD 和 VHS 實體.由于 Book、DVD 和 VHS 類來自不同的應用程序,它們沒有任何祖先實體——共同的祖先是 java.lang.Object.我們仍然希望有一個 Borrow 實體,它可以引用任何可能的媒體類型.
For example let's assume three different applications which manage a media library - the first application manages books borrowing, the second one DVDs, and the third VHSs. The applications have nothing in common. Now we want to develop a new application that manages all three media types and reuses the exiting Book, DVD, and VHS entities. Since Book, DVD, and VHS classes came from different applications they don't have any ancestor entity - the common ancestor is java.lang.Object. Still we would like to have one Borrow entity which can refer to any of the possible media type.
為了解決這種類型的引用,我們可以使用 any 映射.此映射始終包含多個列:一列包含當前映射屬性所引用的實體的類型,另一列包含實體的標識,例如,如果我們引用一本書,它的第一列將包含一個標記Book 實體類型,第二個將包含特定書籍的 id.
To solve this type of references we can use the any mapping. this mapping always includes more than one column: one column includes the type of the entity the current mapped property refers to and the other includes the identity of the entity, for example if we refer to a book it the first column will include a marker for the Book entity type and the second one will include the id of the specific book.
@Entity
@Table(name = "BORROW")
public class Borrow{
@Id
@GeneratedValue
private Long id;
@Any(metaColumn = @Column(name = "ITEM_TYPE"))
@AnyMetaDef(idType = "long", metaType = "string",
metaValues = {
@MetaValue(targetEntity = Book.class, value = "B"),
@MetaValue(targetEntity = VHS.class, value = "V"),
@MetaValue(targetEntity = DVD.class, value = "D")
})
@JoinColumn(name="ITEM_ID")
private Object item;
.......
public Object getItem() {
return item;
}
public void setItem(Object item) {
this.item = item;
}
}
這篇關于如何使用 Hibernate @Any 相關的注解?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!