如何在Hibernate中将一个属性映射到多个属性?

时间:2014-01-17 08:26:11

标签: java database hibernate orm hibernate-mapping

如何在hibernate中将一个属性映射到2个属性?
例如:

@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;
}

现在我想要另一张表ConceptualBook_Author,以便将translatorListModelConceptualBook映射到conceptualBookList ModelAuthor,例如authorList
如何实现此功能?
非常感谢你的帮助;)

1 个答案:

答案 0 :(得分: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;