0、目录
1、关系:多对一
一个Product对应一个Category
一个Category对应多个Product
所以Product和Category是多对一的关系
本例讲解如何使用Hibernate实现多对一关系
2、效果
通过多对一关系,设置了id=8的产品对应 id=1的分类
3、准备Category.java
package com.how2java.pojo;
public class Category {
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;
}
int id;
String name;
}
4、准备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.how2java.pojo">
<class name="Category" table="category_">
<id name="id" column="id">
<generator class="native">
</generator>
</id>
<property name="name" />
</class>
</hibernate-mapping>
5、为Product.java增加Category属性
package com.how2java.pojo;
public class Product {
int id;
String name;
float price;
Category category;
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
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 float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
6、在Product.hbm.xml中设置Category 多对一关系
<many-to-one name="category" class="Category" column="cid" />
使用many-to-one 标签设置多对一关系
name=“category” 对应Product类中的category属性
class=“Category” 表示对应Category类
column=“cid” 表示指向 category_表的外键
<?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.how2java.pojo">
<class name="Product" table="product_">
<id name="id" column="id">
<generator class="native">
</generator>
</id>
<property name="name" />
<property name="price" />
<many-to-one name="category" class="Category" column="cid" />
</class>
</hibernate-mapping>
7、在hibernate.cfg.xml中增加Category的映射
新增Category映射
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=GBK</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/how2java/pojo/Product.hbm.xml" />
<mapping resource="com/how2java/pojo/Category.hbm.xml" />
</session-factory>
</hibernate-configuration>
8、TestHibernate 测试many-to-one关系
在这个测试例子中,增加了一个新的Category对象"c1"
并将其设置为id=8的product的category
package com.how2java.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.how2java.pojo.Category;
import com.how2java.pojo.Product;
public class TestHibernate {
public static void main(String[] args) {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
Category c =new Category();
c.setName("c1");
s.save(c);
Product p = (Product) s.get(Product.class, 8);
p.setCategory(c);
s.update(p);
s.getTransaction().commit();
s.close();
sf.close();
}
}