spring boot - 整合jpa多对对关系保存和查询示例

pojo:

package com.example.zs.springDataJpa;


import org.hibernate.annotations.Proxy;

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

@Entity
@Table(name="t_role")
public class UserRole {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id ;
    @Column(name="name")
    private String name;
    @Column(name="name1")
    private String name1;
    @Column(name="name2")
    private String name2;
    @OneToMany(mappedBy="userRolehh", fetch = FetchType.EAGER)
    private Set<JpaUser> userSet = new HashSet<>();
    @ManyToMany(cascade=CascadeType.PERSIST,fetch=FetchType.EAGER)
    //创建t_role_menu表,PRIMARY KEY (`roleid`,`menuid`),
    // FOREIGN KEY (`roleid`) REFERENCES `t_role` (`id`)
    //FOREIGN KEY (`menuid`) REFERENCES `t_menu` (`menu_id`)
    //不改变t_role表,不改变t_menu表
    @JoinTable(name="t_role_menu",joinColumns =@JoinColumn(name="roleid") ,inverseJoinColumns =@JoinColumn(name="menuid"))
//    private Set<Menu> menuList = new HashSet<>() ; set list都可以
    private List<Menu> menuList = new ArrayList<>() ;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<JpaUser> getUserSet() {
        return userSet;
    }

    public void setUserSet(Set<JpaUser> userSet) {
        this.userSet = userSet;
    }

    public String getName1() {
        return name1;
    }

    public void setName1(String name1) {
        this.name1 = name1;
    }

    public String getName2() {
        return name2;
    }

    public void setName2(String name2) {
        this.name2 = name2;
    }

    public List<Menu> getMenuList() {
        return menuList;
    }
}

 

package com.example.zs.springDataJpa;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Proxy;

import javax.persistence.*;

@Entity
@Table(name="t_user")
public class JpaUser {
//    @Column(name="name")
    private String name ;
//    @Column(name="address")
    private String address ;
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
//    @GeneratedValue(strategy=GenerationType.TABLE,generator="table_gen")
//    @TableGenerator(
//            name = "table_gen",
//            table="fendo_generator",
//            pkColumnName="seq_name",     //指定主键的名字
//            pkColumnValue="fendos",      //指定下次插入主键时使用默认的值
//            valueColumnName="seq_id",    //该主键当前所生成的值,它的值将会随着每次创建累加
//            initialValue = 1,            //初始化值
//            allocationSize=1 )            //累加值
    @Id
    private Integer id ;
//    @Column(name="age1")
    private Integer age ;
//    @Column(name="age11")
    private  Integer age1 ;
//    @ManyToOne(cascade = CascadeType.PERSIST)
    @ManyToOne
    @JoinColumn(name="roleFK")
    private UserRole userRolehh;

    public JpaUser(String name, String address, Integer age, Integer age1 ) {
        this.name = name;
        this.address = address;
        this.age = age;
        this.age1 = age1;
//        this.id = id;
    }

    public JpaUser() {
    }

    @Override
    public String toString() {
        return "JpaUser{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", id=" + id +
                ", age=" + age +
                ", age1=" + age1 +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getAge1() {
        return age1;
    }

    public void setAge1(Integer age1) {
        this.age1 = age1;
    }



    public UserRole getUserRolehh() {
        return userRolehh;
    }

    public void setUserRole(UserRole userRolehh) {
        this.userRolehh = userRolehh;
    }
}
package com.example.zs.springDataJpa;

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

@Entity
@Table(name ="t_menu")
public class Menu {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer menuId ;
    private String menuName ;
    private String menuUrl;
    private Integer fatherId ;
    //menu跟role是多对多关系
    @ManyToMany(mappedBy ="menuList" )
    private Set<UserRole> roleSet = new HashSet<>();
//    private List<UserRole> roleList = new ArrayList<>();

    public void setMenuId(Integer menuId) {
        this.menuId = menuId;
    }

    public void setMenuName(String menuName) {
        this.menuName = menuName;
    }

    public void setMenuUrl(String menuUrl) {
        this.menuUrl = menuUrl;
    }

    public void setFatherId(Integer fatherId) {
        this.fatherId = fatherId;
    }

    public void setRoleSet(Set<UserRole> roleSet) {
        this.roleSet = roleSet;
    }

    public Set<UserRole> getRoleSet() {
        return roleSet;
    }

    public Integer getMenuId() {
        return menuId;
    }
}
package com.example.zs.springDataJpa;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface UserRoleSpecificationExcutor extends JpaRepository<UserRole,Integer> , JpaSpecificationExecutor<UserRole> {
}

保存示例:

  @Test
    public void save () {
        UserRole userRole = new UserRole();
        userRole.setName("角色");
        Menu menu = new Menu();
        Menu menu1 = new Menu();
        userRole.getMenuList().add(menu);
        userRole.getMenuList().add(menu1);
        menu.setMenuName("顶级菜单");
        menu.setMenuUrl("菜单0");
        menu.setFatherId(0);
        menu1.setMenuName("菜单1");
        menu1.setMenuUrl("菜单1");
        menu1.setFatherId(1);

        userRoleSpecificationExcutor.save(userRole);

//        Optional<UserRole>  op = userRoleSpecificationExcutor.findById(1);
//        System.out.println(op);
    }

查询示例:

@Test
public void test2 () {

//    Optional<JpaUser> a= userSpecificationExcutor.findById(13);
    Optional<UserRole> ab =  userRoleSpecificationExcutor.findById(5);
    System.out.println("---");

//        System.out.println(optionalT.get());
//         userRespotory.findByIdxUseHQL("hehe",1);
//        for(JpaUser user : list){
//        }
}

 

转载于:https://www.cnblogs.com/hongchengshise/p/10638531.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值