多对多注解
@ManyToMany
作用
-
用于映射多对多关系
属性
-
cascade:配置级联操作
-
cascade=CascadeType.PERSIST 级联保存
-
cascade=CascadeType.REMOVE 级联删除
-
cascade=CascadeType.ALL 支持所有级联操作
-
-
fetch:配置是否采用延迟加载
-
targetEntity:配置对方的实体类,映射多对多的时候不用写
@JoinTable
作用
-
针对中间表的配置
属性
-
name:配置中间表的名称
-
joinColumns:设置当前实体类和中间表的关联关系
-
name:中间表的外键
-
referencedColumnName:主表的主键
-
-
inverseJoinColumn:设置对方实体类和中间表关联关系
-
name:中间表的外键
-
referencedColumnName:主表的主键
-
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<!-- 要求:在根标签下必须要有一个持久化的单元 (要有一个数据库的连接信息) -->
<persistence-unit name="aaa">
<properties>
<!-- 数据库的连接配置 -->
<!-- 必选 5 -->
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql:///crm"></property>
<property name="hibernate.connection.username" value="root"></property>
<property name="hibernate.connection.password" value="root"></property>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
<!-- 可选 -->
<property name="hibernate.connection.provider_class"
value="org.hibernate.connection.C3P0ConnectionProvider"></property>
<property name="hibernate.show_sql" value="true"></property>
<property name="hibernate.format_sql" value="true"></property>
<property name="hibernate.hbm2ddl.auto" value="update"></property>
</properties>
</persistence-unit>
</persistence>
package com.itheima.domain;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
/**
* @ClassName: Role
* @Description:角色
* @author jsz
* @date 2018年8月20日
*/
@Entity
@Table(name="sys_role")
public class Role {
@Id
@Column(name="role_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long role_id;// 主键ID
@Column(name="role_name")
private String role_name;// '角色名称',
@Column(name="role_memo")
private String role_memo;// '备注',
// 有用户的集合
@ManyToMany(targetEntity=User.class,mappedBy="roles")
private Set<User> users = new HashSet<User>();
public Role(Long role_id, String role_name, String role_memo, Set<User> users) {
super();
this.role_id = role_id;
this.role_name = role_name;
this.role_memo = role_memo;
this.users = users;
}
public Role() {
super();
// TODO Auto-generated constructor stub
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
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;
}
@Override
public String toString() {
return "Role [role_id=" + role_id + ", role_name=" + role_name + ", role_memo=" + role_memo + ", users=" + users
+ "]";
}
}
package com.itheima.domain;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
/**
* @ClassName: User
* @Description:用户
* @author jsz
* @date 2018年8月20日
*/
@Entity
@Table(name = "sys_user")
public class User {
@Id
@Column(name = "user_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long user_id;// '用户id',
@Column(name = "user_code")
private String user_code;// '用户账号',
@Column(name = "user_name")
private String user_name;// '用户名称',
@Column(name = "user_password")
private String user_password;// '用户密码',
@Column(name = "user_state")
private String user_state;// '1:正常,0:暂停',
// 有角色的集合
@ManyToMany(targetEntity = Role.class,cascade=CascadeType.ALL)
@JoinTable(name = "sys_user_role", joinColumns = {
@JoinColumn(name = "user_id", referencedColumnName = "user_id") }, inverseJoinColumns = {
@JoinColumn(name = "role_id", referencedColumnName = "role_id") })
private Set<Role> roles = new HashSet<Role>();
public User(Long user_id, String user_code, String user_name, String user_password, String user_state,
Set<Role> roles) {
super();
this.user_id = user_id;
this.user_code = user_code;
this.user_name = user_name;
this.user_password = user_password;
this.user_state = user_state;
this.roles = roles;
}
public User() {
super();
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
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;
}
@Override
public String toString() {
return "User [user_id=" + user_id + ", user_code=" + user_code + ", user_name=" + user_name + ", user_password="
+ user_password + ", user_state=" + user_state + ", roles=" + roles + "]";
}
}
package com.itheima.test;
import static org.junit.Assert.*;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import org.junit.Test;
import com.itheima.domain.Customer;
import com.itheima.domain.LinkMan;
import com.itheima.domain.Role;
import com.itheima.domain.User;
import com.itheima.utils.JPAUtils;
public class Demo03 {
/**
* @MethodName:test01
* @Description:级联保存:保存用户的时候,保存用户的角色
* @throws Exception
*/
@Test
public void test01() throws Exception {
EntityManager manager = JPAUtils.getEntityManager();
EntityTransaction tx = manager.getTransaction();
tx.begin();
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);
manager.persist(u1);
manager.persist(u2);
tx.commit();
manager.close();
}
/**
* @MethodName:test02
* @Description:删除用户下面的角色
* @throws Exception
*/
@Test
public void test02() throws Exception {
EntityManager manager = JPAUtils.getEntityManager();
EntityTransaction tx = manager.getTransaction();
tx.begin();
Role r2 = manager.find(Role.class, 2l);
User u1 = manager.find(User.class, 1l);
u1.getRoles().remove(r2);
tx.commit();
manager.close();
}
/**
* @MethodName:test03
* @Description:修改用户角色
* @throws Exception
*/
@Test
public void test03() throws Exception {
EntityManager manager = JPAUtils.getEntityManager();
EntityTransaction tx = manager.getTransaction();
tx.begin();
User u2 = manager.find(User.class, 2l);
Role r1 = manager.find(Role.class, 1l);
Role r2 = manager.find(Role.class, 2l);
u2.getRoles().remove(r1);//删除角色1
u2.getRoles().add(r2);//添加角色2
tx.commit();
manager.close();
}
}