简体   繁体   中英

output of many-to-many unidirectional and bidirectional mapping hibernate

In case of one-to-one relationship in hibernate, we can see that there is difference in the output tables in case of unidirectional and bidirectional i,e only one table has foreign key associated with it in unidirectional and both the tables have foreign keys in case of bidirectional. But I cannot see any difference for many-to-many unidirectional and bidirectional output tables. Can anyone help me in this, please? Thanks.

Actually, the number of tables when you use many-to-many unidirectional and bidirectional are the same. The main different is when you use bidirectional one, you can get the list or set objects in both sides. With the unidirectional, you just can get from one side in code. Let's see an example a Book and a Author. A book has many authors and author can write many books. In the example, I used Set but you can use List. It's also fine. Let's see the mapping with unidirectional mapping:

@Entity  
public class Author  
{  
    private Long authorId;  
    private String authorName;  
    private Date dateOfBirth;  

    @Id  
    @GeneratedValue(strategy=GenerationType.AUTO)  
    public Long getAuthorId()  
    {  
        return authorId;  
    }  

    public void setAuthorId(Long authorId)  
    {  
        this.authorId = authorId;  
    }  

    @Column(name="author_name")  
    public String getAuthorName()  
    {  
        return authorName;  
    }  

    public void setAuthorName(String authorName)  
    {  
        this.authorName = authorName;  
    }

    /**
     * @return the dateOfBirth
     */
    public Date getDateOfBirth() {
        return dateOfBirth;
    }
    /**
     * @param dateOfBirth the dateOfBirth to set
     */
    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }
}  


    @Entity  
public class Book  
{  
  private Long bookId;  
  private String bookTitle;  
  private List<Author> authors;  

  @Id  
  @GeneratedValue(strategy=GenerationType.AUTO)  
  public Long getBookId()  
    {  
        return bookId;  
    }  
    public void setBookId(Long bookId)  
    {  
        this.bookId = bookId;  
    }  

    @Column(name="book_title")  
    public String getBookTitle()  
    {  
        return bookTitle;  
    }  
    public void setBookTitle(String bookTitle)  
    {  
        this.bookTitle = bookTitle;  
    }  
    @ManyToMany(cascade=CascadeType.ALL)  
    @JoinTable(name="author_book", joinColumns=@JoinColumn(name="book_id"), inverseJoinColumns=@JoinColumn(name="author_id"))  
    public List<Author> getAuthors()  
    {  
        return authors;  
    }  
    public void setAuthors(List<Author> authors)  
    {  
        this.authors = authors;  
    }  
}  

You can see that the third table is created author_book with the both keys in two table book and author. So, with Unidirectional the Author list can be get from Book only. With the Bidirectional, you can access Books from Author and access Authors from Book. The Book object mapping should be the same. The Author looks a bit different like:

@Entity  
public class Author  
{  

    private Set<Book> books;  

    /**
     * @return the books
     */
    public Set<Book> getBooks() {
        return books;
    }

    /**
     * @param books the books to set
     */
    public void setBooks(Set<Book> books) {
        this.books = books;
    }
//the rest is the same code like above
...

}

Hope it is clear for you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM