一对多映射有两种,一种是单向的,另一种的多向。我们一般是使用双向的,所以我就写写一对多的双向映射。
还是想昨天一样举个例子来说明:作者《===》作品,还是对数据进行增删改查。
我们一般是把外键建立在多方的,一个作者对应多个作品。
这个前一篇的差不多。我就主要写写有差异的地方吧!
还是要建立数据库表,
1 create table t_author( 2 id bigint primary key auto_increment, 3 authorName varchar(20) 4 ); 5 6 create table t_book( 7 id bigint primary key auto_increment, 8 bookName varchar(20), 9 fk_author_id int 10 );
实体:
Author.java
1 package com.cy.beans; 2 3 import java.io.Serializable; 4 import java.util.HashSet; 5 import java.util.Set; 6 7 public class Author implements Serializable { 8 private static final long serialVersionUID = 1L; 9 10 private long id; 11 private String authorName; 12 private Set<Book> books=new HashSet<Book>(); 13 14 public Author(){ 15 16 } 17 18 public long getId() { 19 return id; 20 } 21 22 public void setId(long id) { 23 this.id = id; 24 } 25 26 public String getAuthorName() { 27 return authorName; 28 } 29 30 public void setAuthorName(String authorName) { 31 this.authorName = authorName; 32 } 33 34 35 public Set<Book> getBooks() { 36 return books; 37 } 38 39 public void setBooks(Set<Book> books) { 40 this.books = books; 41 } 42 43 @Override 44 public String toString() { 45 return "Author [id=" + id + ", authorName=" + authorName + 46 ", books=" + books + "]"; 47 } 48 49 50 51 52 }
Book.java
1 package com.cy.beans; 2 3 import java.io.Serializable; 4 5 public class Book implements Serializable { 6 7 private static final long serialVersionUID = 1L; 8 9 private long id; 10 private String bookName; 11 private Author author; 12 public long getId() { 13 return id; 14 } 15 public void setId(long id) { 16 this.id = id; 17 } 18 public String getBookName() { 19 return bookName; 20 } 21 public void setBookName(String bookName) { 22 this.bookName = bookName; 23 } 24 public Author getAuthor() { 25 return author; 26 } 27 public void setAuthor(Author author) { 28 this.author = author; 29 } 30 @Override 31 public String toString() { 32 return "Book [id=" + id + ", bookName=" + bookName + ", author=" 33 + author + "]"; 34 } 35 36 37 }
持久层和业务层都与之前的差不多。
主要映射文件有所改变:
主对象:Author.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping> 7 8 <class name="com.cy.beans.Author" table="t_author" catalog="j2ee"> <!-- catalog数据库 --> 9 10 <id name="id" type="java.lang.Long"><!-- 此行的ID,为对象的属性ID --> 11 <column name="id"></column><!-- 此行的ID,为表字段ID --> 12 <generator class="increment"></generator><!-- 给id指定生成策略 --> 13 </id> 14 15 <property name="authorName" type="java.lang.String"> 16 <column name="authorName"></column> 17 </property> 18 19 <!--lazy :lazy是延时的意思,如果lazy=true,那么就是说数据库中关联子表的信息在hibernate容器启动的时候不会加载,而是在你真正的访问到字表非标识字段的时候,才会去加载。 20 反之,如果lazy=false的话,就是说,子表的信息会同主表信息同时加载 21 Hibernate3.x,lazy默认是true; 22 --> 23 <!-- inverse:hibernate双向关系中的基本概念。inverse的真正作用就是指定由哪一方来维护之间的关联关系。当一方中指定了“inverse=false”(默认),那么那一方就有责任负责之间的关联关系 --> 24 25 <set name="books" table="t_book" cascade="all" inverse="true" lazy="false"><!-- set映射节点 --> 26 <key column="fk_author_id"></key><!-- 外键 --> 27 <one-to-many class="com.cy.beans.Book"/><!--one-to-mang节点 --> 28 </set> 29 </class> 30 31 </hibernate-mapping>
从对象:Book.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 <class name="com.cy.beans.Book" table="t_book" catalog="j2ee"> <!-- catalog数据库 --> 7 8 <id name="id" type="java.lang.Long"><!-- 此行的ID,为对象的属性ID --> 9 <column name="id"></column><!-- 此行的ID,为表字段ID --> 10 <generator class="increment"></generator><!-- 给id指定生成策略 --> 11 </id> 12 13 <property name="bookName" type="java.lang.String"> 14 <column name="bookName"></column> 15 </property> 16 <!-- access:属性值读取方式field, property 17 1,property:这是默认值,表明hibernate通过set和get方法来访问类的属性。这是优先的推荐方式。你必须为这个属性设置get和set方法,hibernate才能访问到。否则会报错的。 18 2,field:表明hibernate通过java的反射机制直接访问类的属性。你可以不给这个属性设置get和set属性。 、 19 fk_author_id :字段名; 20 not-null :外键不为空 21 author :many-to-one 22 --> 23 <many-to-one name="author" class="com.cy.beans.Author" cascade="none" access="property" column="fk_author_id" not-null="true"></many-to-one> 24 </class> 25 </hibernate-mapping>
在hibernate.cfg.xml里添加
1 <mapping resource="com/cy/xmls/Book.hbm.xml"/> 2 <mapping resource="com/cy/xmls/Author.hbm.xml"/>
测试:
1 package com.cy.action; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 import com.cy.beans.Author; 7 import com.cy.beans.Book; 8 import com.cy.beans.Person; 9 import com.cy.server.IAuthorServer; 10 import com.cy.server.IPersonServer; 11 import com.cy.server.impl.AuthorServerImpl; 12 import com.cy.server.impl.PersonServerImpl; 13 14 public class AuthorAction { 15 16 public static void main(String[] args) { 17 // saveAuthor(); 添加 18 updateAuthor();//修改 19 } 20 21 private static void updateAuthor() { 22 IAuthorServer ps=new AuthorServerImpl(); 23 Author p=ps.getAuthor(Author.class, Long.valueOf(1));//查询 24 p.setAuthorName("1111"); 25 ps.updateAuthor(p); 26 27 /*Hibernate: 28 select 29 author0_.id as id1_2_0_, 30 author0_.authorName as authorNa2_2_0_ 31 from 32 j2ee.t_author author0_ 33 where 34 author0_.id=? 35 36 如果在Author.hbm.xml里没有设置lazy="false",那么下面这个select就不会执行 37 Hibernate: 38 select 39 books0_.fk_author_id as fk_autho3_2_0_, 40 books0_.id as id1_3_0_, 41 books0_.id as id1_3_1_, 42 books0_.bookName as bookName2_3_1_, 43 books0_.fk_author_id as fk_autho3_3_1_ 44 from 45 j2ee.t_book books0_ 46 where 47 books0_.fk_author_id=? 48 49 50 51 Hibernate: 52 update 53 j2ee.t_author 54 set 55 authorName=? 56 where 57 id=? 58 Hibernate: 59 update 60 j2ee.t_book 61 set 62 bookName=?, 63 fk_author_id=? 64 where 65 id=?*/ 66 67 } 68 69 private static void saveAuthor() { 70 Author a = new Author(); 71 Book b1 = new Book(); 72 Book b2 = new Book(); 73 74 a.setAuthorName("kitty"); 75 b1.setBookName("book1"); 76 b2.setBookName("book2"); 77 78 IAuthorServer server = new AuthorServerImpl(); 79 Set<Book> books = new HashSet<Book>(); 80 books.add(b2); 81 books.add(b1); 82 b1.setAuthor(a); 83 b2.setAuthor(a); 84 a.setBooks(books); 85 server.saveAuthor(a); 86 87 /*Hibernate: 88 select 89 max(id) 90 from 91 t_author 92 Hibernate: 93 select 94 max(id) 95 from 96 t_book 97 Hibernate: 98 insert 99 into 100 j2ee.t_author 101 (authorName, id) 102 values 103 (?, ?) 104 Hibernate: 105 insert 106 into 107 j2ee.t_book 108 (bookName, fk_author_id, id) 109 values 110 (?, ?, ?) 111 Hibernate: 112 insert 113 into 114 j2ee.t_book 115 (bookName, fk_author_id, id) 116 values 117 (?, ?, ?)*/ 118 119 } 120 }