BaldHead`s Blog

spring data jpa 多对多 ManyToMany

环境搭建

 

导入依赖

 

<properties>
        <spring.version>5.2.5.RELEASE</spring.version>
        <hibernate.version>5.4.10.Final</hibernate.version>
        <slf4j.version>1.7.30</slf4j.version>
        <log4j.version>2.12.1</log4j.version>
        <druid.version>1.1.21</druid.version>
        <mysql.version>5.1.6</mysql.version>

    </properties>

    <dependencies>
        <!-- spring beg -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- spring对orm框架的支持包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- hibernate beg -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.1.2.Final</version>
            <exclusions>
                <exclusion>
                    <artifactId>classmate</artifactId>
                    <groupId>com.fasterxml</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- hibernate end -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <!-- spring data jpa 的坐标 -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.2.6.RELEASE</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--  el beg 使用spring data jpa 必须引入 -->
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.el</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

    </dependencies>

创建配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    <!-- 配置实体类管理工厂 -->
    <bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>

        <!-- 配置实体类的扫描 -->
        <property name="packagesToScan" value="xyz.ytfs.entity"/>


        <!-- 配置jpa的提供方 -->
        <property name="persistenceProvider">
            <bean class="org.hibernate.jpa.HibernatePersistenceProvider"/>
        </property>

        <!-- 配置提供方的适配器 -->
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">

                <!-- 数据库的类型 -->
                <property name="database" value="MYSQL"/>

                <!-- 不自动创表 -->
                <property name="generateDdl" value="false"/>

                <!-- 控制台打印sql语句 -->
                <property name="showSql" value="true"/>

                <property name="databasePlatform" value="org.hibernate.dialect.MySQL55Dialect"/>
            </bean>
        </property>

        <!--  jpa的方言:高级特性 -->
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
        </property>


        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>

    </bean>


    <!-- 配置JPA的事务管理器 -->

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactoryBean"/>
    </bean>

    <!-- JPA -->
    <jpa:repositories base-package="xyz.ytfs.dao" transaction-manager-ref="transactionManager"
                      entity-manager-factory-ref="entityManagerFactoryBean"/>

    <!-- 配置数据源 -->
    <context:property-placeholder location="classpath:jdbcConfig.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

</beans>

创建实体类

注解说明

@ManyToMany
    作用:用于映射多对多关系
    属性:
        cascade:配置级联操作。
        fetch:配置是否采用延迟加载。
        targetEntity:配置目标的实体类。映射多对多的时候不用写。

@JoinTable
    作用:针对中间表的配置
    属性:
        nam:配置中间表的名称
        joinColumns:中间表的外键字段关联当前实体类所对应表的主键字段                          
        inverseJoinColumn:中间表的外键字段关联对方表的主键字段
        
@JoinColumn
    作用:用于定义主键字段和外键字段的对应关系。
    属性:
        name:指定外键字段的名称
        referencedColumnName:指定引用主表的主键字段名称
        unique:是否唯一。默认值不唯一
        nullable:是否允许为空。默认值允许。
        insertable:是否允许插入。默认值允许。
        updatable:是否允许更新。默认值允许。
        columnDefinition:列的定义信息。

 

User

package xyz.ytfs.entity;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @author by ytfs
 * @Classname User
 * @Description TODO(用户类)
 * @Date 2020/5/9 23:47
 */

@Entity
@Table(name = "sys_user")

public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    private Long userId;

    @Column(name = "user_name")
    private String userName;


    /**
     * 配置用户到角色的多对多关系
     * 配置多对多的映射关系
     * 1.声明表关系的配置
     *
     * @ManyToMany(targetEntity = Role.class)  //多对多
     * targetEntity:代表对方的实体类字节码
     * 2.配置中间表(包含两个外键)
     * @JoinTable name : 中间表的名称
     * joinColumns:配置当前对象在中间表的外键
     * @JoinColumn的数组 name:外键名
     * referencedColumnName:参照的主表的主键名
     * inverseJoinColumns:配置对方对象在中间表的外键
     */

    @ManyToMany(targetEntity = Role.class, cascade = CascadeType.ALL)
    @JoinTable(name = "sys_user_role",
            //joinColumns,当前对象在中间表中的外键
            joinColumns = {@JoinColumn(name = "sys_user_id", referencedColumnName = "user_id")},
            //inverseJoinColumns,对方对象在中间表的外键
            inverseJoinColumns = {@JoinColumn(name = "sys_role_id", referencedColumnName = "role_id")})
    private Set<Role> roles = new HashSet<>();

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", roles=" + roles +
                '}';
    }
}

Role

package xyz.ytfs.entity;


import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @author by ytfs
 * @Classname Role
 * @Description TODO(用户对应的类型)
 * @Date 2020/5/9 23:47
 */

@Entity
@Table(name = "sys_role")
public class Role {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "role_id")
    private Long roleId;


    @Column(name = "role_name")
    private String roleName;


    /**
     * 配置用户到角色的多对多关系
     *      配置多对多的映射关系
     *          1.声明表关系的配置
     *              @ManyToMany(targetEntity = User.class)  //多对多
     *                  targetEntity:代表对方的实体类字节码
     *          2.配置中间表(包含两个外键)
     *                @JoinTable
     *                  name : 中间表的名称
     *                  joinColumns:配置当前对象在中间表的外键
     *                      @JoinColumn的数组
     *                          name:外键名
     *                          referencedColumnName:参照的主表的主键名
     *                  inverseJoinColumns:配置对方对象在中间表的外键
     */
     /*  @ManyToMany(targetEntity = User.class)
        @JoinTable(name = "sys_user_role",
            //joinColumns,当前对象在中间表中的外键
            joinColumns = {@JoinColumn(name = "sys_role_id", referencedColumnName = "role_id")},
            //inverseJoinColumns,对方对象在中间表的外键
            inverseJoinColumns = {@JoinColumn(name = "sys_user_id", referencedColumnName = "user_id")})*/
    @ManyToMany(mappedBy = "roles")
    private Set<User> users = new HashSet<>();

    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

    public Long getRoleId() {
        return roleId;
    }

    public void setRoleId(Long roleId) {
        this.roleId = roleId;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    @Override
    public String toString() {
        return "Role{" +
                "roleId=" + roleId +
                ", roleName='" + roleName + '\'' +
                ", users=" + users +
                '}';
    }
}

创建数据访问层的接口

IUserDao

package xyz.ytfs.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import xyz.ytfs.entity.User;

/**
 * @author by ytfs
 * @Classname UserDao
 * @Description TODO(用户的数据访问层接口)
 * @Date 2020/5/10 0:05
 */


public interface IUserDao extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
}

IRoleDao

package xyz.ytfs.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import xyz.ytfs.entity.Role;

/**
 * @author by ytfs
 * @Classname IRoleDao
 * @Description TODO(用户对应的职位数据访问层)
 * @Date 2020/5/10 0:07
 */


public interface IRoleDao extends JpaRepository<Role, Long>, JpaSpecificationExecutor<Role> {
}

测试类

package xyz.ytfs.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import xyz.ytfs.dao.IRoleDao;
import xyz.ytfs.dao.IUserDao;
import xyz.ytfs.entity.Role;
import xyz.ytfs.entity.User;

import javax.transaction.Transactional;
import java.util.HashSet;

/**
 * @author by ytfs
 * @Classname JpaManyToManyTest
 * @Description TODO(多对多的测试类)
 * @Date 2020/5/10 0:08
 */


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class JpaManyToManyTest {


    @Autowired
    private IRoleDao roleDao;

    @Autowired
    private IUserDao userDao;

    /**
     * 保存一个用户,保存一个角色
     * <p>
     * 多对多放弃维护权:被动的一方(这里就是被选择的一方Role)放弃
     */
    @Test
    @Transactional
    @Rollback(false)
    public void testSave() {

        User user = new User();
        user.setUserName("雨听风说");


        Role role = new Role();
        role.setRoleName("程序猿");

         /*
         表达多对多的关系,双方都表达的时候必须有一方放弃维护中间表,不然会抛出异常
          */
        role.getUsers().add(user);

        user.getRoles().add(role);


        this.userDao.save(user);
        this.roleDao.save(role);

    }

    //测试级联添加(保存一个用户的同时保存用户的关联角色)
    @Test
    @Transactional
    @Rollback(false)
    public void testCasacdeSave() {
        User user = new User();
        user.setUserName("雨听风说");


        Role role = new Role();
        role.setRoleName("程序猿");

        /*
         表达多对多的关系
          */
        role.getUsers().add(user);

        user.getRoles().add(role);

        //级联操作,在保存用户的同时保存用户的类型
        this.userDao.save(user);

    }

    /**
     * 级联删除,删除id为1的客户
     */

    @Test
    @Transactional
    @Rollback(false)
    public void testDeleteCasaced() {
        //查询id为1的用户
        User user = this.userDao.getOne(1L);

        //删除id为1的用户,级联删除用户对应的类型
        this.userDao.delete(user);
    }

}

 

posted @ 2020-05-12 15:12  BaldHead  阅读(1530)  评论(0编辑  收藏  举报