2014-01-17 87 views
0

如何在hibernate中將一個屬性映射到2個屬性?
例子:如何在Hibernate中將一個屬性映射到多個屬性?

@Entity 
@Table(name = "Author") 
public class ModelAuthor extends Model { 

    @ManyToMany(mappedBy = "authorList", 
       fetch = FetchType.LAZY) 
    private Set<ModelConceptualBook> conceptualBookList; 
} 


@Entity 
@Table(name = "ConceptualBook") 
public class ModelConceptualBook extends Model { 

    @ManyToMany(fetch = FetchType.LAZY) 
    @JoinTable(name = "ConceptualBook_Author", 
       joinColumns = {@JoinColumn(name = "conceptualBookId")}, 
       inverseJoinColumns = {@JoinColumn(name = "authorId")}) 
    private Set<ModelAuthor> authorList; 

    private Set<ModelAuthor> translatorList; 
} 

現在我想有anaother表ConceptualBook_Author映射translatorListModelConceptualBookModelAuthorauthorListconceptualBookList
如何實現此功能?
非常感謝您的幫助;)

回答

0

通過簡單地使用其他表 - 例如ConceptualBook_Translator

@ManyToMany(fetch = FetchType.LAZY) 
@JoinTable(name = "ConceptualBook_Translator", 
      joinColumns = {@JoinColumn(name = "conceptualBookId")}, 
      inverseJoinColumns = {@JoinColumn(name = "authorId")}) 
private Set<ModelAuthor> translatorList; 

而在另一邊的關係如下:

@ManyToMany(mappedBy = "translatorList", 
       fetch = FetchType.LAZY) 
    private Set<ModelConceptualBook> booksTranslated; 
+0

非常感謝您的幫助,但我能寫的「@ManyToMany(的mappedBy =‘而不是authorList’,取= FetchType .LAZY)「爲」ModelAuthor「類中的」conceptualBookList「? – user3205721

+0

已添加到答覆。它不是,而是作爲現有的補充。這些是相同類型之間的兩種不同關係,角色:翻譯者和角色:作者。如果需要將兩個集合合併爲一個並返回的附加方法當然可以添加。 –

相關問題