Spring Data JPA - Relational Query - Operating Multi-to-Multi Relational Relations

Keywords: xml

One) Bidirectional multi-to-many configuration

1.1: Configure many-to-many
Requirement roles have multiple menus, and menus belong to multiple roles
Annotation @ManyToMany used

@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ManyToMany {
    Class targetEntity() default void.class; //Target class, referring to the associated target class

    CascadeType[] cascade() default {};//cascade policies

    FetchType fetch() default FetchType.LAZY;//Loading strategy

    String mappedBy() default "";//mapping
}

Menu entity

@Entity
@Table(name = "t_menu")
public class Menu {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "menuid")
    private Integer menuid;


    @Column(name = "menuName")
    private String menuName;


    @ManyToMany(mappedBy = "menuSet")
    private Set<Roles> rolesSet = new HashSet<>();

Role entity

Annotation @ManyToMany. @JoinTable
The @JoinTable annotation is the annotation to set up the intermediate table, and the @JoinTable annotation can appear in either of the two parties.
@ JoinTable requires three main attributes
1.name: name of the intermediate table
2. join Columns: The foreign key name of the intermediate table that exists in the current table
3. InverseJoin Columns Another name for a multi-party intermediate table

@Entity
@Table(name = "t_roles")
@Lazy(false)
public class Roles {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "roleid")
    private Integer roleid;

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

    /**
     * One-to-many configuration
     */
    @OneToMany(mappedBy = "roles",fetch = FetchType.EAGER)
    private Set<Users> usersSet = new HashSet<>();


    /**
     * Many-to-many configuration: Annotations can appear in either of two multi-party entities, so in this case, the configuration is reversed from the current configuration.
     * Use the annotation @JoinTable:name: specify the name of the intermediate table
     *                      joinColumns: Current multiparty presence of foreign key names in the middle table
     *                      inverseJoinColumns: The foreign key name of another party that exists in the middle table
     */
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "role_menu",joinColumns = @JoinColumn(name = "role_id"),inverseJoinColumns = @JoinColumn(name = "menu_id"))
    private Set<Menu> menuSet = new HashSet<>();

test

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext-hib.xml")
public class TestManyToMany {

    @Autowired
    private RoleDao roleDao;

    @Test
    public void test2(){
        Roles one = this.roleDao.findOne(5);
        System.out.println(one);
        one.getMenuSet().stream().forEach(System.out::println);
    }


    @Test
    public void test1(){
        Roles roles = new Roles();
        roles.setRoleName("SuperManager");

        Menu menu = new Menu();
        menu.setMenuName("PageManager");

        Menu menu1 = new Menu();
        menu.setMenuName("PesonManager");

        //Establish relationship
        roles.getMenuSet().add(menu);
        roles.getMenuSet().add(menu1);

        menu.getRolesSet().add(roles);

        //Action menu through role Association
        this.roleDao.save(roles);
    }
}

Posted by NTFS on Mon, 30 Sep 2019 23:56:27 -0700