先是两个实体类的创建,这里我简单用物品类和订单类进行演示
package com.qiu.many_to_many; //此处自己的工程包可以忽略
import java.util.Set;
/**
* @author 邱
* @2017年9月20日 多对多 备注:物品类
*/
public class Goods {
private long id;
private String type; //商品类型
private String name;
private double price;
private Set<Order> set;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Set<Order> getSet() {
return set;
}
public void setSet(Set<Order> set) {
this.set = set;
}
}
订单的实体类
package com.qiu.many_to_many;
import java.util.Set;
/**
* @author 邱
* @2017年9月20日
* 备注: 订单类 多对多
*/
public class Order {
private long id;//订单编号
private double money;
private String address;
private Set<Goods> set;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Set<Goods> getSet() {
return set;
}
public void setSet(Set<Goods> set) {
this.set = set;
}
}
实体类比较简单,大致看一下即可,主要是配置文件的编写
物品类的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!--
-->
<!--约束文件-->
<!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.qiu.many_to_many">
<class name="Goods" table="tb_goods">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name" length="20"/>
<property name="type" length="10"/>
<property name="price" />
<set name="set" table="tb_orderitem">
<key column="gid"></key>
<many-to-many class="Order" column="oid"></many-to-many>
</set>
</class>
</hibernate-mapping>
订单的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!--
-->
<!--约束文件-->
<!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.qiu.many_to_many">
<class name="Order" table="tb_order">
<id name="id">
<generator class="native"></generator>
</id>
<property name="money" />
<property name="address" length="50"/>
<!--多对多 -->
<!--集合类型的属性
name:属性名
table:关系表的表名 -->
<set name="set" table="tb_orderitem">
<!--当前的类在中间表的外键的名称 -->
<key column="oid"></key>
<!--集合的泛型的类型,标记与谁是多对多,并且指示中间表中的外键名称 -->
<many-to-many class="Goods" column="gid"></many-to-many>
</set>
</class>
</hibernate-mapping>
全局配置文件把 四本一言搞清楚一般不会错,记得把各个类的映射配置文件路径加上
<?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工厂的设置,其实就是连接设置 -->
<session-factory>
<!--四本一言
四大基本标签:驱动、url、用户名、密码
一言:方言,标记是哪一种数据库 -->
<!--1、数据库驱动类名全称 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!--2、数据库的路径 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_1710_moretable?characterEncoding=UTF-8</property>
<!--3、数据库用户名 -->
<property name="hibernate.connection.username">root</property>
<!--4、密码 -->
<property name="hibernate.connection.password">123456</property>
<!--5、方言,一定要和数据库版本一致 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property>
<!--建表语句的执行:create\update -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--显示自动生成的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!--格式化sql语句 -->
<property name="hibernate.format_sql">true</property>
<!--启用安全Session,就是可以调用getCurrentSession -->
<property name="hibernate.current_session_context_class">thread</property>
<!--多对多外键映射 -->
<mapping resource="com/qiu/many_to_many/Goods.hbm.xml" />
<mapping resource="com/qiu/many_to_many/Order.hbm.xml" />
</session-factory>
</hibernate-configuration>
下面进行一些简单的演示
package com.qiu.many_to_many;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.qiu.utils.HibernateUtils;
/**
* @author 邱
* @2017年9月20日
* 备注:多对多的演示
*/
public class MyTest {
private Session session;
private Transaction tran;
// 每次测试方法执行之前调用
@Before
public void bef() {
session = HibernateUtils.openSession();
tran = session.beginTransaction();
}
// 每次测试方法结束后调用
@After
public void aft() {
tran.commit();
session.close();
}
//多对多 新增:
@Test
public void test1(){
Goods goods = new Goods();
goods.setName("月饼");
goods.setPrice(9.9);
goods.setType("糕点");
Order order = new Order();
order.setMoney(9.9);
order.setAddress("xxxxxxxxxxx");
//这里只需要添加一次即可,否则重复添加
Set<Goods> set2 = new HashSet<>();
set2.add(goods);
order.setSet(set2);
System.out.println("新增:"+ session.save(order));
System.out.println("新增:"+ session.save(goods));
}
//多对多查询
@Test
public void test2(){
Order order2 = session.get(Order.class, 1l);
System.out.println(order2.getAddress());
for(Goods g : order2.getSet()){
System.out.println(g.getName()+" "+g.getType());
}
Goods goods=session.get(Goods.class, 1l);
System.out.println(goods.getName()+" "+goods.getPrice()+"----->");
for(Order o : goods.getSet()){
System.out.println(o.getAddress()+" "+o.getMoney());
}
}
}