使用的数据
建表语句
CREATE DATABASE Hibernate02;
USE Hibernate02;
CREATE TABLE `cut_customer`(
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
`cust_user_id` bigint(32) DEFAULT NULL COMMENT '负责人ID',
`cust_create_id` bigint(32) DEFAULT NULL COMMENT '创建人ID',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
`cust_linkman` varchar(64) DEFAULT NULL COMMENT '联系人',
`cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
`cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
PRIMARY KEY (`cust_id`)
)ENGINE=InnoDB AUTO_INCREMENT =1 DEFAULT CHARSET = UTF8;
CREATE TABLE `cst_linkman`(
`link_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '联系人编号(主键)',
`link_name` varchar(16) DEFAULT NULL COMMENT '联系人姓名',
`link_cust_id` bigint(32) DEFAULT NULL COMMENT '客户ID',
`link_gender` char(1) DEFAULT NULL COMMENT '联系人性别',
`link_phone` varchar(16) DEFAULT NULL COMMENT '联系人办公电话',
`link_mobile` varchar(16) DEFAULT NULL COMMENT '联系人手机',
`link_email` varchar(64) DEFAULT NULL COMMENT '联系人邮箱',
`link_qq` varchar(16) DEFAULT NULL COMMENT '联系人QQ',
`link_position` varchar(16) DEFAULT NULL COMMENT '联系人职位',
`link_memo` varchar(512) DEFAULT NULL COMMENT '联系人备注',
PRIMARY KEY (`link_id`),
KEY `FK_cst_linkman_lkm_cust_id` (`link_cust_id`),
CONSTRAINT `FK_cst_linkman_lkm_cust_id` FOREIGN KEY (`link_cust_id`) REFERENCES `cut_customer` (`cust_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT =1 DEFAULT CHARSET = UTF8;
hibernate.cfg.xml
<?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>
<!--
#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate02?serverTimezone=UTC</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<!-- 可选配置 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!--
#hibernate.c3p0.max_size 2
#hibernate.c3p0.min_size 2
#hibernate.c3p0.timeout 5000
#hibernate.c3p0.max_statements 100
#hibernate.c3p0.idle_test_period 3000
#hibernate.c3p0.acquire_increment 2
#hibernate.c3p0.validate false
-->
<property name="hibernate.c3p0.max_size">3</property>
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.timeout">5000</property>
<!--
hibernate.format_sql true
#hibernate.show_sql true
#hibernate.hbm2ddl.auto create-drop
#hibernate.hbm2ddl.auto create
#hibernate.hbm2ddl.auto update
#hibernate.hbm2ddl.auto validate
-->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 将session绑定到当前线程,使用getCurrentSession 获取当前线程 -->
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="com/igeek/domain/Customer.hbm.xml"/>
<mapping resource="com/igeek/domain/LinkMan.hbm.xml"/>
</session-factory>
</hibernate-configuration>
使用的实体类
/**
*
*/
package com.igeek.domain;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
/**
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class Customer implements Serializable{
private Long cust_id;
private String cust_name;
private Long cust_user_id;
private Long cust_create_id;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
//使用set集合可以表示多个联系人
private Set<LinkMan> linkMains = new HashSet<LinkMan>();
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public Long getCust_user_id() {
return cust_user_id;
}
public void setCust_user_id(Long cust_user_id) {
this.cust_user_id = cust_user_id;
}
public Long getCust_create_id() {
return cust_create_id;
}
public void setCust_create_id(Long cust_create_id) {
this.cust_create_id = cust_create_id;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
public Set<LinkMan> getLinkMains() {
return linkMains;
}
public void setLinkMains(Set<LinkMan> linkMains) {
this.linkMains = linkMains;
}
@Override
public String toString() {
return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_user_id=" + cust_user_id
+ ", cust_create_id=" + cust_create_id + ", cust_source=" + cust_source + ", cust_industry="
+ cust_industry + ", cust_level=" + cust_level + ", cust_linkman=" + cust_linkman + ", cust_phone="
+ cust_phone + ", cust_mobile=" + cust_mobile + "]";
}
}
/**
*
*/
package com.igeek.domain;
import java.io.Serializable;
/**
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class LinkMan implements Serializable{
private Long link_id;
private String link_name;
//客户外键不需要我们自己编写
// private Long link_cust_id;
private String link_gender;
private String link_phone;
private String link_mobile;
private String link_email;
private String link_qq;
private String link_position;
private String link_memo;
private Customer customer;
public Long getLink_id() {
return link_id;
}
public void setLink_id(Long link_id) {
this.link_id = link_id;
}
public String getLink_name() {
return link_name;
}
public void setLink_name(String link_name) {
this.link_name = link_name;
}
// public Long getLink_cust_id() {
// return link_cust_id;
// }
// public void setLink_cust_id(Long link_cust_id) {
// this.link_cust_id = link_cust_id;
// }
public String getLink_gender() {
return link_gender;
}
public void setLink_gender(String link_gender) {
this.link_gender = link_gender;
}
public String getLink_phone() {
return link_phone;
}
public void setLink_phone(String link_phone) {
this.link_phone = link_phone;
}
public String getLink_mobile() {
return link_mobile;
}
public void setLink_mobile(String link_mobile) {
this.link_mobile = link_mobile;
}
public String getLink_email() {
return link_email;
}
public void setLink_email(String link_email) {
this.link_email = link_email;
}
public String getLink_qq() {
return link_qq;
}
public void setLink_qq(String link_qq) {
this.link_qq = link_qq;
}
public String getLink_position() {
return link_position;
}
public void setLink_position(String link_position) {
this.link_position = link_position;
}
public String getLink_memo() {
return link_memo;
}
public void setLink_memo(String link_memo) {
this.link_memo = link_memo;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@Override
public String toString() {
return "LinkMan [link_id=" + link_id + ", link_name=" + link_name + ", link_gender=" + link_gender
+ ", link_phone=" + link_phone + ", link_mobile=" + link_mobile + ", link_email=" + link_email
+ ", link_qq=" + link_qq + ", link_position=" + link_position + ", link_memo=" + link_memo
+ ", customer=" + customer + "]";
}
}
使用的持久化类配置
Customer.hbm.xml
<?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>
<!--
`cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
`cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
`cust_user_id` bigint(32) DEFAULT NULL COMMENT '负责人ID',
`cust_create_id` bigint(32) DEFAULT NULL COMMENT '创建人ID',
`cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
`cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
`cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
`cust_linkman` varchar(64) DEFAULT NULL COMMENT '联系人',
`cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
`cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
-->
<class name="com.igeek.domain.Customer" table="cut_customer">
<id name="cust_id" column="cust_id">
<generator class="native"></generator>
</id>
<property name="cust_name" column="cust_name"></property>
<property name="cust_user_id" column="cust_user_id" />
<property name="cust_create_id" column="cust_create_id"></property>
<property name="cust_source" column="cust_source"></property>
<property name="cust_industry" column="cust_industry"></property>
<property name="cust_level" column="cust_level"></property>
<property name="cust_linkman" column="cust_linkman"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
<!--
设置多方关系
name:javaBean 中set 集合的名字
key :column 外键的名字
one-to-many class:set 集合中类的全路径
-->
<!-- 让瞬时态对象变成持久态
cascade :
save-update 配置级联保存
delete 配置级联删除,当配置级联删除后,一方删除,相关联的数据将会被删除
delete-orphan 孤儿删除
-->
<set name="linkMains" >
<key column="link_cust_id" ></key>
<one-to-many class="com.igeek.domain.LinkMan"
/>
</set>
</class>
</hibernate-mapping>
LinkMan.hbm.xml
<?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>
<class name="com.igeek.domain.LinkMan" table="cst_linkman">
<!--
`link_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '联系人编号(主键)',
`link_name` varchar(16) DEFAULT NULL COMMENT '联系人姓名',
`link_cust_id` bigint(32) DEFAULT NULL COMMENT '客户ID',
`link_gender` char(1) DEFAULT NULL COMMENT '联系人性别',
`link_phone` varchar(16) DEFAULT NULL COMMENT '联系人办公电话',
`link_mobile` varchar(16) DEFAULT NULL COMMENT '联系人手机',
`link_email` varchar(64) DEFAULT NULL COMMENT '联系人邮箱',
`link_qq` varchar(16) DEFAULT NULL COMMENT '联系人QQ',
`link_position` varchar(16) DEFAULT NULL COMMENT '联系人职位',
`link_memo` varchar(512) DEFAULT NULL COMMENT '联系人备注',
-->
<id name="link_id" column="link_id">
<generator class="native"></generator>
</id>
<property name="link_name" column="link_name"></property>
<!-- <property name="link_cust_id" column="link_cust_id"></property> -->
<property name="link_gender" column="link_gender"></property>
<property name="link_phone" column="link_phone"></property>
<property name="link_mobile" column="link_mobile"></property>
<property name="link_email" column="link_email"></property>
<property name="link_qq" column="link_qq"></property>
<property name="link_position" column="link_position"></property>
<property name="link_memo" column="link_memo"></property>
<!--
设置多对一
name :javaBean 的属性名
class:属性类的全路径
column: 外键的名称
-->
<!--
把瞬时态的客户变成持久态
cascade:
save-update 配置级联保存
delete 配置级联删除,当配置级联删除后,一方删除,相关联的数据将会被删除
-->
<many-to-one name="customer"
class="com.igeek.domain.Customer"
column="link_cust_id"
></many-to-one>
</class>
</hibernate-mapping>
1.测试双向关联:
/***
* 双向关联
*/
@Test
public void testSave()
{
Session session = HibernateUtil.getCurrentSession();
Transaction transaction = session.beginTransaction();
//新建一个客户和两个联系人
Customer c = new Customer();
c.setCust_name("张晓");
LinkMan m = new LinkMan();
m.setLink_name("王敏");
LinkMan m2 = new LinkMan();
m2.setLink_name("宋晓");
//双向关联 客户关联联系人
c.getLinkMains().add(m);
c.getLinkMains().add(m2);
//联系人关联客户
m.setCustomer(c);
m2.setCustomer(c);
//保存
session.save(m2);
session.save(m);
session.save(c);
transaction.commit();
//不需要手动关闭session
}
2.测试单向关联
一对多方式操作:
修改 Customer.hbm.xml 持久化类,设置set集合属性
cascade=“save-update”,其它与双向关联保持一致
<!--
设置多方关系
name:javaBean 中set 集合的名字
key :column 外键的名字
one-to-many class:set 集合中类的全路径
-->
<!-- 让瞬时态对象变成持久态
cascade :
save-update 配置级联保存
delete 配置级联删除,当配置级联删除后,一方删除,相关联的数据将会被删除
delete-orphan 孤儿删除
-->
<set name="linkMains"
cascade="save-update"
>
<key column="link_cust_id" ></key>
<one-to-many class="com.igeek.domain.LinkMan"
/>
</set>
测试类
/***
* 单向关联 从客户保存联系人
*/
@Test
public void testSingle()
{
Session session = HibernateUtil.getCurrentSession();
Transaction transaction = session.beginTransaction();
Customer c = new Customer();
c.setCust_name("韦若然");
LinkMan m1 = new LinkMan();
m1.setLink_name("张少华");
LinkMan m2 = new LinkMan();
m2.setLink_name("张辉元");
c.getLinkMains().add(m2);
c.getLinkMains().add(m1);
//保存
session.save(c);
transaction.commit();
}
多对一的操作(在数据表中,数据级联操作,一对多的单向操作,操纵的次数较多,所以建议使用多对一的操作,同时列出上述方法,配置持久化类时,操作那个类,配置那个类,比如现在配置LinkMan.hbm.xml)
修改 LinkMan.hbm.xml 持久化类,设置many-to-one标签属性cascade=“save-update”,其它与双向关联保持一致
<!--
设置多对一
name :javaBean 的属性名
class:属性类的全路径
column: 外键的名称
-->
<!--
把瞬时态的客户变成持久态
cascade:
save-update 配置级联保存
delete 配置级联删除,当配置级联删除后,一方删除,相关联的数据将会被删除
-->
<many-to-one name="customer"
class="com.igeek.domain.Customer"
column="link_cust_id"
cascade="save-update"
></many-to-one>
测试类
/***
* 单向关联 从联系人保存客户
*
*/
@Test
public void testSingle2()
{
Session session = HibernateUtil.getCurrentSession();
Transaction transaction = session.beginTransaction();
LinkMan m1 = new LinkMan();
m1.setLink_name("王晓文");
LinkMan m2 = new LinkMan();
m2.setLink_name("王晓涵");
Customer c = new Customer();
c.setCust_name("邵燕");
//单项关联
//联系人关联客户
m1.setCustomer(c);
m2.setCustomer(c);
//单项关联
session.save(m1);
session.save(m2);
transaction.commit();
}
3.孤儿删除(删除一对多中,多的一方某一条数据)
Customer.hbm.xml 中设置set 属性 cascade=“delete-orphan” 其它与双向关联一致
<!--
设置多方关系
name:javaBean 中set 集合的名字
key :column 外键的名字
one-to-many class:set 集合中类的全路径
-->
<!-- 让瞬时态对象变成持久态
cascade :
save-update 配置级联保存
delete 配置级联删除,当配置级联删除后,一方删除,相关联的数据将会被删除
delete-orphan 孤儿删除
-->
<set name="linkMains" cascade="delete-orphan" >
<key column="link_cust_id" ></key>
<one-to-many class="com.igeek.domain.LinkMan"
/>
</set>
测试类
/***
* 孤儿删除
*/
@Test
public void testDelete2()
{
Session session = HibernateUtil.getCurrentSession();
Transaction tran = session.beginTransaction();
//获取一个客户
Customer c = session.get(Customer.class, 11L);
//获取2号联系人
LinkMan m2 = session.get(LinkMan.class, 11L);
//删除关系
c.getLinkMains().remove(m2);
tran.commit();
}
4.级联删除
Customer.hbm.xml 中设置set 属性 cascade=“delete” 其它与双向关联一致
<!--
设置多方关系
name:javaBean 中set 集合的名字
key :column 外键的名字
one-to-many class:set 集合中类的全路径
-->
<!-- 让瞬时态对象变成持久态
cascade :
save-update 配置级联保存
delete 配置级联删除,当配置级联删除后,一方删除,相关联的数据将会被删除
delete-orphan 孤儿删除
-->
<set name="linkMains" cascade="delete" >
<key column="link_cust_id" ></key>
<one-to-many class="com.igeek.domain.LinkMan"
/>
</set>
测试类
@Test
public void testDelete1()
{
Session session = HibernateUtil.getCurrentSession();
Transaction tran = session.beginTransaction();
Customer c = session.get(Customer.class, 9L);
session.delete(c);
tran.commit();
}
多对多的数据操作:
如:人员和角色两张表,人员1可以扮演角色1,人员1可以扮演角色2,人员2可以扮演角色1,人员2可以扮演角色2,那么就需要第三张表(中间表)来存储它们之间的关联
CREATE TABLE `sys_user`(
`user_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '用户id',
`user_code` varchar(32) DEFAULT NULL COMMENT '用户账号',
`user_name` varchar(64) DEFAULT NULL COMMENT '用户名称',
`user_password` varchar(32) DEFAULT NULL COMMENT '用户密码',
`user_state` char(1) DEFAULT NULL COMMENT '1:正常,0:暂停',
PRIMARY KEY(`user_id`)
) ENGINE = InnoDB AUTO_INCREMENT = 9 DEFAULT CHARSET = UTF8;
CREATE TABLE `sys_role`(
`role_id` BIGINT(32) DEFAULT NULL AUTO_INCREMENT,
`role_name` varchar(32) DEFAULT NULL COMMENT '角色名称',
`role_memo` varchar(128) DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`role_id`)
)ENGINE = InnoDB AUTO_INCREMENT = 6 DEFAULT CHARSET = UTF8;
当前的实体类
/**
*
*/
package com.igeek.domain;
import java.util.HashSet;
import java.util.Set;
/**
* @author Administrator
*
*/
public class User {
private Long user_id;
private String user_code;
private String user_name;
private String user_password;
private String user_state;
//使用set 集合存放多个角色
private Set<Role> roles = new HashSet<Role>();
public Long getUser_id() {
return user_id;
}
public void setUser_id(Long user_id) {
this.user_id = user_id;
}
public String getUser_code() {
return user_code;
}
public void setUser_code(String user_code) {
this.user_code = user_code;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_password() {
return user_password;
}
public void setUser_password(String user_password) {
this.user_password = user_password;
}
public String getUser_state() {
return user_state;
}
public void setUser_state(String user_state) {
this.user_state = user_state;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
/**
*
*/
package com.igeek.domain;
import java.util.HashSet;
import java.util.Set;
/**
* @author Administrator
*
*/
public class Role {
private Long role_id;
private String role_name;
private String role_memo;
//使用set集合来存放多个用户
private Set<User> users = new HashSet<User>();
public Long getRole_id() {
return role_id;
}
public void setRole_id(Long role_id) {
this.role_id = role_id;
}
public String getRole_name() {
return role_name;
}
public void setRole_name(String role_name) {
this.role_name = role_name;
}
public String getRole_memo() {
return role_memo;
}
public void setRole_memo(String role_memo) {
this.role_memo = role_memo;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
持久化类
<?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>
<class name="com.igeek.domain.Role" table="sys_role">
<id name="role_id" column="role_id">
<generator class="native"></generator>
</id>
<property name="role_name" column="role_name"></property>
<property name="role_memo" column="role_memo"></property>
<!--
配置中间表信息,当前的类实现的是双向保存,设置inverse=true 可以不关联,直接在保存User中添加Role中关联信息就可以(减少数据库数据重复插入/更新操作)
-->
<set name="users" table="sys_user_role"
inverse="true"
>
<!-- 角色在中间表的外键 -->
<key column="role_id"></key>
<!-- 配置用户的映射文件及在中间表的外键 -->
<many-to-many class="com.igeek.domain.User"
column="user_id">
</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>
<class name="com.igeek.domain.User" table="sys_user">
<id name="user_id" column="user_id">
<generator class="native"></generator>
</id>
<property name="user_code" column="user_code"></property>
<property name="user_name" column="user_name"></property>
<property name="user_password" column="user_password"></property>
<property name="user_state" column="user_state"></property>
<!--
配置
set 集合描述User类中的role 属性和中间表信息
name:user类 角色中的属性
table 中间表
key
column 中间表中用户的外键
many-to-many
class role 实体类的路径
column 表sys_user 的 外键
-->
<set name="roles" table="sys_user_role"
>
<key column="user_id"></key>
<many-to-many class="com.igeek.domain.Role"
column="role_id">
</many-to-many>
</set>
</class>
</hibernate-mapping>
Hibernate.cfg.xml 中配置
<?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>
<!--
#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
-->
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate02?serverTimezone=UTC</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<!-- 可选配置 -->
<!-- <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!--
#hibernate.c3p0.max_size 2
#hibernate.c3p0.min_size 2
#hibernate.c3p0.timeout 5000
#hibernate.c3p0.max_statements 100
#hibernate.c3p0.idle_test_period 3000
#hibernate.c3p0.acquire_increment 2
#hibernate.c3p0.validate false
-->
<property name="hibernate.c3p0.max_size">3</property>
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.timeout">5000</property>
<!--
hibernate.format_sql true
#hibernate.show_sql true
#hibernate.hbm2ddl.auto create-drop
#hibernate.hbm2ddl.auto create
#hibernate.hbm2ddl.auto update
#hibernate.hbm2ddl.auto validate
-->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 将session绑定到当前线程,使用getCurrentSession 获取当前线程 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- <mapping resource="com/igeek/domain/Customer.hbm.xml"/>
<mapping resource="com/igeek/domain/LinkMan.hbm.xml"/> -->
<mapping resource="com/igeek/domain/User.hbm.xml"/>
<mapping resource="com/igeek/domain/Role.hbm.xml"/>
</session-factory>
</hibernate-configuration>
测试类:
@Test
public void save()
{
//新建两个用户
User u1 = new User();
u1.setUser_name("张晓");
User u2 = new User();
u2.setUser_name("王雯");
//新建两个角色
Role r1 = new Role();
r1.setRole_name("演员");
Role r2 = new Role();
r2.setRole_name("歌手");
//表之间的关联
u1.getRoles().add(r1);
u1.getRoles().add(r2);
u2.getRoles().add(r1);
u2.getRoles().add(r2);
Session session = HibernateUtil.getCurrentSession();
Transaction transaction = session.beginTransaction();
session.save(u1);
session.save(u2);
session.save(r1);
session.save(r2);
transaction.commit();
}