• Spring Data JPA:关联映射操作


    1.一对一的关系关联

    需求:用户和角色一对一关联

    复制代码
    package com.example.jpa.pojo;
    
    import javax.persistence.*;
    
    @Entity
    @Table(name = "t_user")
    public class User {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private Long id;
        @Column(name = "name")
        private String name;
        /**
         * CascadeType.PERSIST 只有User类新增时,
         * 会级联Role对象新增。若Role对象在数据库存(跟新)在则抛异常(让Role变为持久态)
         */
        @OneToOne(cascade = CascadeType.PERSIST)
        @JoinColumn(name = "role_id") //JoinColumn:维护一个外键的作用
        private Role role;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Role getRole() {
            return role;
        }
    
        public void setRole(Role role) {
            this.role = role;
        }
    }
    复制代码
    复制代码
    package com.example.jpa.pojo;
    
    import javax.persistence.*;
    
    @Entity
    @Table(name = "t_role")
    public class Role {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private Long id;
        @Column(name = "name")
        private String name;
        @OneToOne(mappedBy = "role")
        private User user;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public User getUser() {
            return user;
        }
    
        public void setUser(User user) {
            this.user = user;
        }
    }
    复制代码
    复制代码
     /**
         * 添加用户同时添加角色
         */
        @Test
        public void test1()
        {
            //创建角色
            Role role =new Role();
            role.setName("小角色");
    
            //创建用户
            User user =new User();
            user.setName("小用户");
    
            //建立关系
            user.setRole(role);
            role.setUser(user);
    
            //保存数据
            userRepository.save(user);
        }
    
        /**
         * 根据用户id查询用户,同时查询用户角色
         */
        @Test
        public void test2()
        {
            User user =userRepository.getOne((long)13);
            System.out.println(user);
            Role role = user.getRole();
            System.out.println(role);
    
        }
    复制代码

    2.一对多关联关系

    需求:从角色到用户的一对多关联关系

    复制代码
    package com.example.jpa.pojo;
    
    import javax.persistence.*;
    
    @Entity
    @Table(name = "t_user")
    public class User {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private Long id;
        @Column(name = "name")
        private String name;
    
        @ManyToOne(cascade = CascadeType.PERSIST)
        @JoinColumn(name = "role_id")
        private Role role;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Role getRole() {
            return role;
        }
    
        public void setRole(Role role) {
            this.role = role;
        }
    }
    复制代码
    复制代码
    package com.example.jpa.pojo;
    
    import javax.persistence.*;
    import java.util.HashSet;
    import java.util.Set;
    
    @Entity
    @Table(name = "t_role")
    public class Role {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private Long id;
        @Column(name = "name")
        private String name;
    
        @OneToMany(mappedBy = "role")
        private Set<User> userSet = new HashSet<>();
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Set<User> getUserSet() {
            return userSet;
        }
    
        public void setUserSet(Set<User> userSet) {
            this.userSet = userSet;
        }
    }
    复制代码
    复制代码
     /**
         * 添加用户同时添加角色
         */
        @Test
        public void test1()
        {
            //创建角色
            Role role =new Role();
            role.setName("小角色");
    
            //创建用户
            User user =new User();
            user.setName("小用户");
    
            //建立关系
            user.setRole(role);
            role.getUserSet().add(user);
    
            //保存数据
            userRepository.save(user);
        }
    
        /**
         * 根据用户id查询用户,同时查询用户角色
         */
        @Test
        public void test2()
        {
            User user =userRepository.getOne((long)13);
            System.out.println(user);
            Role role = user.getRole();
            System.out.println(role);
    
        }
    复制代码

    3.多对多的关联关系

    需求:用户和角色多对多关系

    复制代码
    package com.example.jpa.pojo;
    
    import javax.persistence.*;
    import java.util.HashSet;
    import java.util.Set;
    
    @Entity
    @Table(name = "t_user")
    public class User {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private Long id;
        @Column(name = "name")
        private String name;
    
        @ManyToMany(cascade = CascadeType.PERSIST,fetch = FetchType.EAGER) //fetch = FetchType.EAGER:异步加载
        //JoinTable 配置中间表信息
        //joinColumns 建立当前表在中间表中的外键字段
        @JoinTable(name = "t_user_role",joinColumns = @JoinColumn(name = "user_id")
                ,inverseJoinColumns = @JoinColumn(name = "role_id"))
       private Set<Role> roles =new HashSet<>();
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Set<Role> getRoles() {
            return roles;
        }
    
        public void setRoles(Set<Role> roles) {
            this.roles = roles;
        }
    }
    复制代码
    复制代码
    package com.example.jpa.pojo;
    
    import javax.persistence.*;
    import java.util.HashSet;
    import java.util.Set;
    
    @Entity
    @Table(name = "t_role")
    public class Role {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private Long id;
        @Column(name = "name")
        private String name;
    
        @ManyToMany(mappedBy = "roles")
        private Set<User> users =new HashSet<>();
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Set<User> getUsers() {
            return users;
        }
    
        public void setUsers(Set<User> users) {
            this.users = users;
        }
    }
    复制代码
    复制代码
        /**
         * 添加用户同时添加角色
         */
        @Test
        public void test1()
        {
            //创建角色
            Role role =new Role();
            role.setName("小角色");
    
            //创建用户
            User user1 =new User();
            user1.setName("小用户1");
    
            User user2 =new User();
            user2.setName("小用户2");
    
    
            //建立关系
            role.getUsers().add(user1);
            role.getUsers().add(user2);
            user1.getRoles().add(role);
            user2.getRoles().add(role);
    
            //保存数据
            userRepository.save(user1);
            userRepository.save(user2);
        }
    
        /**
         * 根据用户id查询用户,同时查询用户所有角色
         */
        @Test
        public void test2()
        {
            User user =userRepository.getOne((long)13);
            System.out.println(user);
            Set<Role> roles = user.getRoles();
            System.out.println(roles);
    
        }
    复制代码
  • 相关阅读:
    C# new()约束简单介绍
    sql serverHow can I output more than 256 characters to a file?
    sql serverHow can I output more than 256 characters to a file?
    程序员必须掌握的核心算法有哪些?
    程序员必须掌握的核心算法有哪些?
    C#How to use less than sign and greater than sign symbols in xml query?
    C#How to use less than sign and greater than sign symbols in xml query?
    SQLServer 查询消息输出不全 print输出结果不全
    SQLServer 查询消息输出不全 print输出结果不全
    创建计算字段
  • 原文地址:https://www.cnblogs.com/zouhong/p/11643146.html
Copyright © 2020-2023  润新知