一种Product可以被多个User购买
一个User可以购买多种Product
所以Product和User之间的关系是多对多 many-to-many
要实现多对多关系,必须有一张中间表 user_product 用于维护 User和Product之间的关系
步骤1:先运行,看到效果,再学习
步骤2:模仿和排错
步骤3:User.java
步骤4:User.hbm.xml
步骤5:Product.java
步骤6:Product.hbm.xml
步骤7:在hibernate.cfg.xml中增加User的映射
步骤8:TestHibernate 测试many-to-many关系
步骤 1 : 先运行,看到效果,再学习
老规矩,先下载下载区(点击进入)的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
步骤 2 : 模仿和排错
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。
推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
步骤 3 : User.java
package com.how2java.pojo;
import java.util.Set;
public class User {
int id;
String name;
Set<Product> products;
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 Set<Product> getProducts() {
return products;
}
public void setProducts(Set<Product> products) {
this .products = products;
}
}
|
步骤 4 : User.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 = "User" table = "user_" >
< id name = "id" column = "id" >
< generator class = "native" >
</ generator >
</ id >
< property name = "name" />
< set name = "products" table = "user_product" lazy = "false" >
< key column = "uid" />
< many-to-many column = "pid" class = "Product" />
</ set >
</ class >
</ hibernate-mapping >
|
步骤 5 : Product.java
增加了对应Users的集合
package com.how2java.pojo;
import java.util.Set;
public class Product {
int id;
String name;
float price;
Category category;
Set<User> users;
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this .users = users;
}
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
<? 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" />
< set name = "users" table = "user_product" lazy = "false" >
< key column = "pid" />
< many-to-many column = "uid" class = "User" />
</ set >
</ class >
</ hibernate-mapping >
|
步骤 7 : 在hibernate.cfg.xml中增加User的映射
<? 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" />
< mapping resource = "com/how2java/pojo/User.hbm.xml" />
</ session-factory >
</ hibernate-configuration >
|
步骤 8 : TestHibernate 测试many-to-many关系
首先增加3个用户
然后演示产品1被用户1,2,3购买
package com.how2java.test;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.how2java.pojo.Product;
import com.how2java.pojo.User;
public class TestHibernate {
public static void main(String[] args) {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
//增加3个用户
Set<User> users = new HashSet();
for ( int i = 0 ; i < 3 ; i++) {
User u = new User();
u.setName( "user" +i);
users.add(u);
s.save(u);
}
//产品1被用户1,2,3购买
Product p1 = (Product) s.get(Product. class , 1 );
p1.setUsers(users);
s.save(p1);
s.getTransaction().commit();
s.close();
sf.close();
}
}
|
关注我,每天准时更新Java技术知识
更多Java全栈内容,点击了解: https://how2j.cn/k/hibernate/hibernate-many-to-many/42.html