JPA Retrieve Data By Using Many-to-Many Relation
In the previous section, you had read about the many-to-many relation.
Here, you will learn how to retrieve data to database table by using the JPA many-to-many relation. @ManyToMany Annotation provides the facility to relate data many-to-many to database table. In this case, you need to insert, update and retrieve many data to many tables. To use java.util.set for this operation.
As follow the following steps:
Table author:
Table book:
Table author_book:
Model Class:
Author.java
/** * */ package roseindia; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinTable; import javax.persistence.JoinColumn; import javax.persistence.ManyToMany; import javax.persistence.Table; /** * @author Administrator * */ @Entity @Table(name="author") public class Author { @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") private int id; /** * @return the id */ public int getId() { return id; } /** * @param id the id to set */ public void setId(int id) { this.id = id; } @Column(name="authorName", nullable=false, length=50, insertable=true) private String authorName; /** * @return the authorName */ public String getAuthorName() { return authorName; } /** * @param authorName the authorName to set */ public void setAuthorName(String authorName) { this.authorName = authorName; } //for joing the tables (many-to-many) @ManyToMany(cascade=CascadeType.ALL) @JoinTable(name = "author_book", joinColumns = { @JoinColumn(name="authorId") }, inverseJoinColumns = { @JoinColumn(name="bookId") } ) 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; } } |
Book.java
/** * */ package roseindia; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; /** * @author Administrator * */ @Entity @Table(name="book") public class Book { @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") private int id; /** * @return the id */ public int getId() { return id; } /** * @param id the id to set */ public void setId(int id) { this.id = id; } @Column(name="bookName", nullable=false, length=250, insertable=true) private String bookName; /** * @return the bookName */ public String getBookName() { return bookName; } /** * @param bookName the bookName to set */ public void setBookName(String bookName) { this.bookName = bookName; } //for joing the tables (many-to-many) @ManyToMany(cascade=CascadeType.ALL) @JoinTable(name = "author_book", joinColumns = { @JoinColumn(name="bookId") }, inverseJoinColumns = { @JoinColumn(name="authorId") } ) private Set<Author> authors; /** * @return the authors */ public Set<Author> getAuthors() { return authors; } /** * @param authors the authors to set */ public void setAuthors(Set<Author> authors) { this.authors = authors; } } |
Main Class:
ManyToManyRelation1.java
/** * */ package roseindia; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; /** * @author Administrator * */ public class ManyToManyRelation1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub EntityManagerFactory emf=null; EntityManager em=null; try{ emf=Persistence.createEntityManagerFactory("default", new HashMap()); em=emf.createEntityManager(); em.getTransaction().begin(); Author author = new Author(); author.setAuthorName("Clifford Geertz"); Author author1 = new Author(); author1.setAuthorName("JP Morgenthal"); Book book = new Book(); book.setBookName("Phoenix"); Book book1 = new Book(); book1.setBookName("Enterprise Applications Integration with XML and Java"); HashSet bookSet = new HashSet(); bookSet.add(book); bookSet.add(book1); author.setBooks(bookSet); author1.setBooks(bookSet); em.persist(author); em.persist(author1); Author authorRecord = em.find(Author.class, author.getId()); System.out.println("Author: "+ authorRecord.getAuthorName()); Set<Book> bookset = authorRecord.getBooks(); Iterator it = bookset.iterator(); while(it.hasNext()){ Book b = (Book)it.next(); System.out.println("Book: "+ b.getBookName()); } em.getTransaction().commit(); System.out.println("Done"); } catch(Exception e){ System.out.println(e.getMessage()); } finally{ em.close(); emf.close(); } } } |
Output:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version). log4j:WARN Please initialize the log4j system properly. Hibernate: insert into author (authorName) values (?) Hibernate: insert into book (bookName) values (?) Hibernate: insert into book (bookName) values (?) Hibernate: insert into author (authorName) values (?) Author: Clifford Geertz Book: Phoenix Book: Enterprise Applications Integration with XML and Java Hibernate: insert into author_book (authorId, bookId) values (?, ?) Hibernate: insert into author_book (authorId, bookId) values (?, ?) Hibernate: insert into author_book (authorId, bookId) values (?, ?) Hibernate: insert into author_book (authorId, bookId) values (?, ?) Done 0
|