Hibernate JPA 配置多对多问题

SysUser.java

@ManyToMany(fetch = FetchType.EAGER, targetEntity = SysRole.class)
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@JoinTable(name = "SYS_USER_ROLE", 
        joinColumns={ @JoinColumn(name="USER_ID",referencedColumnName="ID")},
        inverseJoinColumns={@JoinColumn(name="ROLE_ID",referencedColumnName="ID")})
private Set<SysRole> roles;

** SysRole.java**

    @ManyToMany(fetch = FetchType.EAGER, targetEntity = SysMenu.class)
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@JoinTable(name = "SYS_ROLE_MENU", schema="VIDS",
        joinColumns = { @JoinColumn(name = "ROLE_ID", referencedColumnName = "ID") }, 
        inverseJoinColumns = { @JoinColumn(name = "MENU_NAME", referencedColumnName = "MENU_NAME") })
private Set<SysMenu> menus;
以上就是我的所有配置
测试:
UserService userService  =  (UserService)SpringContextUtils.getBean("userService");
    SysUser user = userService.getUserByUsername("zhangsan");
    System.out.print(":::::"+user.getUserName()+"\t");
    Iterator<SysRole> roles = user.getRoles().iterator();
    while (roles.hasNext()) {
        SysRole role = roles.next();
        System.out.print(role.getName()+"\t");
            Iterator<SysMenu> menus = role.getMenus().iterator();
            while (menus.hasNext()) {
                SysMenu menu = menus.next();
                System.out.print(menu.getName()+"\t");
            }
            System.out.println();
        }
    }

    输出结果
    :::::zhangsan   admin   
    为毛menus取不到?
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐