用户和角色是多对多的关系。那么他们需要一个中间表来维持多对多的关系。中间表有两个外键分别指向用户表和角色表的主键。中间没有单独的主键,uid和rid作为联合主键。
如果我们自己创建,就要创建三张表。
如果使用hibernate,只要编写2个javabean,编写2个映射的配置文件,中间表会自动生成。
一、javabean和配置
1.1 javabean
User.java
package com.hib.domain;
import java.util.HashSet;
import java.util.Set;
public class User {
private Long uid;
private String username;
private String password;
//多对多,编写的都是集合,集合要自己new哦
private Set<Role> roles = new HashSet<>();
public Long getUid() {
return uid;
}
public void setUid(Long uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
Role.java
package com.hib.domain;
import java.util.HashSet;
import java.util.Set;
public class Role {
private Long rid;
private String rname;
//多对多,编写的都是集合,集合要自己new哦
private Set<User> users = new HashSet<>();
public Long getRid() {
return rid;
}
public void setRid(Long rid) {
this.rid = rid;
}
public String getRname() {
return rname;
}
public void setRname(String rname) {
this.rname = rname;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
1.2 配置
User.hbm2.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.hib.domain.User" table="sys_user">
<id name="uid" column="uid">
<generator class="native" />
</id>
<property name="username" column="username" />
<property name="password" column="password" />
<!-- 只要javabean中有集合,在配置中就要配 -->
<!-- 配置多对多: name——集合的名称; table——中间表的名称 -->
<set name="roles" table="sys_user_role">
<!-- 当前表在中间表的外键的名称 -->
<key column="uid" />
<!-- class——集合中存入对象,对象的全路径;column——集合中对象在中间表的外键的名称 -->
<many-to-many class="com.hib.domain.Role" column="rid" />
</set>
</class>
</hibernate-mapping>
Role.hbm2.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.hib.domain.Role" table="sys_role">
<id name="rid" column="rid">
<generator class="native" />
</id>
<property name="rname" column="rname" />
<!-- 多对多必须有一方放弃外键的维护,建议,哪方操作少,就在哪方放弃 -->
<set name="users" table="sys_user_role" inverse="true">
<key column="rid" />
<many-to-many class="com.hib.domain.User" column="uid" />
</set>
</class>
</hibernate-mapping>
二、测试程序
package com.hib.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* hibernate框架的工具类
*
*/
public class HibernateUtils {
private static final Configuration CONFIG;
private static final SessionFactory FACTORY;
// 静态代码块,类在加载的时候就会执行
static {
// 加载xml的配置文件
CONFIG = new Configuration().configure();
// 构造工厂
FACTORY = CONFIG.buildSessionFactory();
}
public static Session getSession() {
return FACTORY.openSession();
}
/**
* 业务层开事务,就用这个
*
* @return
*/
public static Session getCurrentSession() {
// 从ThreadLocal类中获取到session对象
return FACTORY.getCurrentSession();
}
public static void main(String[] args) {
getSession();
}
}
执行main方法
三、运行结果
四、保存程序
@Test
public void run1() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
// 模拟多对多,双向的关联
// 创建用户
User u1 = new User();
u1.setUsername("zhangsan");
User u2 = new User();
u2.setUsername("lisi");
// 创建角色
Role r1 = new Role();
r1.setRname("manager");
Role r2 = new Role();
r2.setRname("actor");
// 关联
u1.getRoles().add(r1);
u1.getRoles().add(r2);
r1.getUsers().add(u1);
r2.getUsers().add(u1);
u2.getRoles().add(r1);
r1.getUsers().add(u2);
session.save(u1);
session.save(u2);
session.save(r1);
session.save(r2);
tx.commit();
}
五、保存结果
看sql:
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hibernate:
insert
into
sys_user
(username, password)
values
(?, ?)
Hibernate:
insert
into
sys_user
(username, password)
values
(?, ?)
Hibernate:
insert
into
sys_role
(rname)
values
(?)
Hibernate:
insert
into
sys_role
(rname)
values
(?)
Hibernate:
insert
into
sys_user_role
(uid, rid)
values
(?, ?)
Hibernate:
insert
into
sys_user_role
(uid, rid)
values
(?, ?)
Hibernate:
insert
into
sys_user_role
(uid, rid)
values
(?, ?)