1、写一个实体类
package hibernate;
import java.util.HashSet;
import java.util.Set;
public class User {
private Integer user_id;
private String user_name;
private String user_password;
private Set<Role> roles = new HashSet<Role>();
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public Integer getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this.user_id = user_id;
}
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;
}
}
2、再写一个实体类
package hibernate;
import java.util.HashSet;
import java.util.Set;
public class Role {
private Integer role_id;
private String role_name;
private String role_memo;
private Set<User> users = new HashSet<User>();
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public Integer getRole_id() {
return role_id;
}
public void setRole_id(Integer 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;
}
}
3、类的映射关系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="hibernate.User" table="user">
<id name="user_id" column="user_id">
<generator class="native"></generator>
</id>
<property name="user_name" column="user_name"></property>
<property name="user_password" column="user_password"></property>
<set name="roles" table="user_role" cascade="save-update,delete">
<!-- column是外键名称 -->
<key column="userid"></key>
<many-to-many class="hibernate.Role" column="roleid"></many-to-many>
</set>
</class>
</hibernate-mapping>
4、另一个类的映射关系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="hibernate.Role" table="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>
<set name="users" table="user_role">
<!-- column是外键名称 -->
<key column="roleid"></key>
<many-to-many class="hibernate.User" column="userid"></many-to-many>
</set>
</class>
</hibernate-mapping>
5、核心配置文件
<?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>
<!-- 1、配置数据库信息 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 2、配置hibernate信息 可选的-->
<!-- 输出底层sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 对底层sql语句格式化 -->
<property name="hibernate.format_sql">true</property>
<!-- hibernate帮创建表
update:如果已经有表,更新,如果没有,创建-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置数据库方言
让hibernate识别不同数据库语句-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 3、把映射文件放到核心配置文件中 -->
<mapping resource="hibernate/User.hbm.xml"></mapping>
<mapping resource="hibernate/Role.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
6、测试文件
package hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class TestHibernate {
@Test
public void test()
{
//1、加载hibernate核心配置文件
Configuration cfg = new Configuration();
cfg.configure();
//2、创建SessionFactory对象
//读取核心配置文件,创建sessionFactory
//根据映射关系创建表
SessionFactory sessionFacroty = cfg.buildSessionFactory();
//3、创建session对象
//类似于创建连接
Session session = sessionFacroty.openSession();
//4、开启事务
org.hibernate.Transaction tx = session.beginTransaction();
//5、写具体crud操作
User u1 = new User();
u1.setUser_name("lucy");
u1.setUser_password("123");
User u2 = new User();
u2.setUser_name("mary");
u2.setUser_password("456");
Role r1 = new Role();
r1.setRole_memo("总经理");
r1.setRole_name("总经理");
Role r2 = new Role();
r2.setRole_memo("秘书");
r2.setRole_name("秘书");
Role r3 = new Role();
r3.setRole_memo("保安");
r3.setRole_name("保安");
//u1 -- r1/r2
//u2 -- r2/r3
u1.getRoles().add(r1);
u1.getRoles().add(r2);
u2.getRoles().add(r2);
u2.getRoles().add(r3);
session.save(u1);
session.save(u2);
//6、提交事务
tx.commit();
//7、关闭资源
session.close();
sessionFacroty.close();
}
}