This explains the List collection mapping maps
1. Typically for collection, processing using the hibernate are set to complete. But also it provides other mapping hibernate for several collections.
List mapping implemented here, List are ordered set, so there is a need for a sequence of data in the table.
2. Mapping commonly found in many collections, the use case is the category and book
3. The class structure
Book.java
public class Book implements Serializable{ private int id; private String name; private String author; private double price; private Date pubDate; public Book() { } public Book(String name, String author, double price, Date pubDate) { super(); this.name = name; this.author = author; this.price = price; this.pubDate = pubDate; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public Date getPubDate() { return pubDate; } public void setPubDate(Date pubDate) { this.pubDate = pubDate; } @Override public String toString() { return "Book [id=" + id + ", name=" + name + ", author=" + author + ", price=" + price + ", pubDate=" + pubDate + "]"; } }
Category.java
public class Category implements Serializable{ private int id; private String name; private List<Book> books = new ArrayList<>(); public Category() { } public Category(String name) { super(); this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Book> getBooks() { return books; } public void setBooks(List<Book> books) { this.books = books; } }
4. mapping file
Book.hbm.xml
<hibernate-mapping package="cn.sxt.pojo"> <class name="Book" table="t_book"> <id name="id"> <generator class="native"></generator> </id> <property name="name"/> <property name="author"/> <property name="price"/> <property name="pubDate"/> </class> </hibernate-mapping>
Category.hbm.xml
<hibernate-mapping package="cn.sxt.pojo"> <class name="Category" table="t_category"> <id name="id"> <generator class="native"></generator> </id> <property name="name"/> <!-- 一对多的关联映射 --> <list name="books"> <key column="cid"></key> <!-- idx有hibernate来进行维护 --> <index column="idx"/> <one-to-many class="Book"/> </list> </class> </hibernate-mapping>
5. Test
public class HibernateTest { / ** * means a method for generating database tables * * / @Test public void testCreateDB () { the Configuration CFG = new new the Configuration () Configure ();. the SchemaExport SE = new new the SchemaExport (CFG); // first sql script print parameters whether // if the second parameter to the execution script to export database se.create ( to true , to true ); } / ** * initialize table data mode * used to hold many data, performs updated foreign key update statement * efficiency lower than that of many-efficient way * / @Test public void testInit(){ Session session = null; Transaction tx = null; try { session = HibernateUtil.getSession(); tx = session.beginTransaction(); Category c1 = new Category("计算机类"); Category c2 = new Category("文学"); Category c3 = new Category("历史"); SimpleDateFormat df = newThe SimpleDateFormat ( "the MM-dd-YYYY" ); Book B1 = new new Book ( "Java", "Sun", 30, df.parse ( "1995-05-23" )); Book B2 = new new Book ( "Struts" "Apache", 40, df.parse ( "2006-09-12" )); Book B3 = new new Book ( "Ming those thing", "then the moon", 70, df.parse ( "2008-05 -23 " )); Book B4 = new new Book (" "," Human tear old ", 20 is, df.parse (" 1985-05-23 " )); / / set the relationship . c1.getBooks () add ( B1); . c1.getBooks () the Add (B2); c2.getBooks (). add (b4) ; c3.getBooks().add(b3); session.save(c1); Session.save (C2); Session.save (C3); Session.save (B1); Session.save (B2); Session.save (B3); Session.save ( B4); tx.commit (); } the catch (Exception E) { IF ! (TX = null ) tx.rollback (); } the finally { HibernateUtil.close (); } } / ** * number of queries in the end of the data data may be obtained when one end of a * / @Test public void testGetData () { the Session the session = HibernateUtil.getSession(); Category c1 = (Category)session.get(Category.class, 1); System.out.println(c1.getId()+"----"+c1.getName()); System.out.println("-----------------"); for(Book book:c1.getBooks()){ System.out.println(book); } HibernateUtil.close(); } }