一个Product对应一个Category
一个Category对应多个Product
所以Product和Category是多对一的关系
本例讲解如何使用Hibernate实现多对一关系
第一步 准备Category.java
1 package com.ghw.pojo; 2 3 public class Category { 4 5 public int getId() { 6 return id; 7 } 8 public void setId(int id) { 9 this.id = id; 10 } 11 public String getName() { 12 return name; 13 } 14 public void setName(String name) { 15 this.name = name; 16 } 17 int id; 18 String name; 19 }
第二步 准备Category.hbm.xml
1 <?xml version="1.0"?> 2 3 <!DOCTYPE hibernate-mapping PUBLIC 4 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 5 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 6 <hibernate-mapping package="com.ghw.pojo"> 7 <class name="Category" table="category_"> 8 <id name="id" column="id"> 9 <generator class="native"> 10 </generator> 11 </id> 12 <property name="name" /> 13 </class> 14 </hibernate-mapping>
第三步 在Product.java中添加Category属性和它的setter和getter方法
1 package com.ghw.pojo; 2 public class Product { 3 private int id; 4 private String name; 5 private float price; 6 private Category category; 7 public Category getCategory() { 8 return category; 9 } 10 public void setCategory(Category category) { 11 this.category = category; 12 } 13 public int getId() { 14 return id; 15 } 16 public void setId(int id) { 17 this.id = id; 18 } 19 public String getName() { 20 return name; 21 } 22 public void setName(String name) { 23 this.name = name; 24 } 25 public float getPrice() { 26 return price; 27 } 28 public void setPrice(float price) { 29 this.price = price; 30 } 31 }
第四步 在Hibernate.hbm.xml文件中添加Category.hbm.xml的mappering映射
新增Category映射
1 <?xml version=‘1.0‘ encoding=‘utf-8‘?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 7 <session-factory> 8 <!-- Database connection settings --> 9 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 10 <property name="connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=GBK</property> 11 <property name="connection.username">root</property> 12 <property name="connection.password">admin</property> 13 <!-- SQL dialect --> 14 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 15 <property name="current_session_context_class">thread</property> 16 <property name="show_sql">true</property> 17 <property name="hbm2ddl.auto">update</property> 18 <mapping resource="com/ghw/pojo/Product.hbm.xml" /> 19 <mapping resource="com/ghw/pojo/Category.hbm.xml" /> 20 </session-factory> 21 22 </hibernate-configuration>
第五步 在Product.hbm.xml中设置Category 多对一关系
many-to-one中
name="category" 对应Product类中的category属性
class="Category" 表示对应Category类
column="cid" 表示指向 category_表的外键
1 <?xml version="1.0"?> 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 package="com.ghw.pojo"> 7 <class name="Product" table="product_"> 8 <id name="id" column="id"> 9 <generator class="native"> 10 </generator> 11 </id> 12 13 <property name="name" /> 14 <property name="price" /> 15 <many-to-one name="category" class="Category" column="cid" /> 16 </class> 17 18 </hibernate-mapping>
第六步 TestHibernate 测试many-to-one关系
一定要先保存c再保存p,否则报错抛出异常。
1 package com.how2java.test; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.cfg.Configuration; 6 7 import com.ghw.pojo.Category; 8 import com.ghw.pojo.Product; 9 10 public class TestHibernate { 11 public static void main(String[] args) { 12 SessionFactory sf = new Configuration().configure().buildSessionFactory(); 13 14 Session s = sf.openSession(); 15 s.beginTransaction(); 16 17 Category c =new Category(); 18 c.setName("c1"); 19 s.save(c); 20 21 Product p = (Product) s.get(Product.class, 8); 22 p.setCategory(c); 23 s.update(p); 24 25 s.getTransaction().commit(); 26 s.close(); 27 sf.close(); 28 } 29 }