Question

How can I map one property to 2 properties in hibernate ?
Example:

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

Now I want to have anaother table ConceptualBook_Author for mapping translatorList from ModelConceptualBook to conceptualBookList of ModelAuthor like authorList.
How can I achieve this functionality?
Thanks a lot for your help ;)

Was it helpful?

Solution

By simply using other table - for example ConceptualBook_Translator.

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

And on the other side relationship is as follows:

   @ManyToMany(mappedBy = "translatorList", 
                fetch = FetchType.LAZY)
    private Set<ModelConceptualBook> booksTranslated;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top