1. 程式人生 > >hibernate 多對一雙向對映關係

hibernate 多對一雙向對映關係

Book類(圖書類):

package com.hc.model;

public class Book {
	private Integer id;			
	private String name;		
	private String author;		
	private Category category;	
	public Integer getId() {
		return id;
	}
	public void setId(Integer 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 Category getCategory() {
		return category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}
}
Category類(圖書類別類,多本圖書對應一種類別):
package com.hc.model;

public class Category {
	private Integer id;			
	private String name;		
	private Set<Book> books;	//Set集合(類別中的所有圖書)
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Set<Book> getBooks() {
		return books;
	}
	public void setBooks(Set<Book> books) {
		this.books = books;
	}
}

Book表:


Category表:


Book對映檔案(Book.hbm.xml):

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hc.model">
	<class name="Book" table="book_manytoone">
		<!-- 主鍵 -->
		<id name="id">
			<generator class="native"/>
		</id>
		<!-- 圖書名稱 -->
		<property name="name" not-null="true" length="200" />
		<!-- 作者 -->
		<property name="author" not-null="true" length="50"/>
		<!-- 多對一關聯對映 -->
		<many-to-one name="category" class="Category" cascade="save-update">
			<!-- 對映的欄位 -->
			<column name="categoryId"/>
		</many-to-one>
	</class>
</hibernate-mapping>
Category對映檔案(Category.hbm.xml):
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping  package="com.wgh.model">
	<class name="Category" table="tb_Category_manytoone">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" not-null="true" length="200" />
		<!-- 一對多對映 -->
		<set name="books">
			<key column="categoryId"/>
			<one-to-many class="Book"/>
		</set>
		
	</class>
</hibernate-mapping>
測試:
Session session = null;			//宣告Session物件
		try {
			//獲取Session
			session = HibernateUtil.getSession();
			//開啟事務
			session.beginTransaction();
			System.out.println("*********查詢圖書物件************");
			//查詢圖書物件
			Book book1 = (Book)session.get(Book.class, new Integer(1));
			System.out.println("圖書名稱:" + book1.getName());
			System.out.println("圖書類別:" + book1.getCategory().getName());
			System.out.println("*********查詢類別物件************");
			//查詢類別物件
			Category c = (Category) session.load(Category.class, new Integer(1));
			System.out.println("類別名稱:" + c.getName());
			Set<Book> books = c.getBooks();		//獲取類別中的所有圖書
			//通過迭代輸出圖書名稱
			for (Iterator<Book> it = books.iterator(); it.hasNext();) {
				Book book2 = (Book) it.next();
				System.out.println("圖書名稱:" + book2.getName());
			}
			//提交事務
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			//出錯將回滾事務
			session.getTransaction().rollback();
		}finally{
			//關閉Session物件
			HibernateUtil.closeSession();
		}