Spring引导,CrudRepository findbyid或findOne没有得到角色表的一对多表格细节

时间:2017-03-06 03:33:01

标签: java spring hibernate spring-boot spring-data-jpa

@Entity
@NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private Long  id;

    private String email;

    private String password;

    private String username;

    @Transient
    private String passwordConfirm;

    //bi-directional many-to-one association to Role
    @ManyToOne(fetch=FetchType.LAZY ,cascade = {CascadeType.PERSIST})
    private Role role;

    public User() {
    }

    public Long  getId() {
        return this.id;
    }

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

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Role getRole() {
        return this.role;
    }

    public void setRole(Role role) {
        this.role = role;
    }


    @Transient
    public String getPasswordConfirm() {
        return passwordConfirm;
    }

    public void setPasswordConfirm(String passwordConfirm) {
        this.passwordConfirm = passwordConfirm;
    }


}




@Entity
@NamedQuery(name="Role.findAll", query="SELECT r FROM Role r")
public class Role implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private Long id;

    private String name;

    //bi-directional many-to-one association to User
    @OneToMany(fetch=FetchType.LAZY, mappedBy="role", cascade = {CascadeType.PERSIST})
    private List<User> users;

    public Role() {
    }

    public Long  getId() {
        return this.id;
    }

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

    public String getName() {
        return this.name;
    }

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

    public List<User> getUsers() {
        return this.users;
    }

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

    public User addUser(User user) {
        getUsers().add(user);
        user.setRole(this);

        return user;
    }

    public User removeUser(User user) {
        getUsers().remove(user);
        user.setRole(null);

        return user;
    }

}
public interface UserRepository extends CrudRepository <User, Long> {
public UserDto getUserDetailsById(Long userId)  throws commonException  {
        //ArrayList<UserDto> arr = new ArrayList<>();   
        User user = userRepository.`findOne`(userId);

            UserDto userDto = new UserDto();
            userDto.setId(user.getId());
            userDto.setUsername(user.getUsername());
            userDto.setEmail(user.getEmail());
            userDto.setPassword(user.getPassword());
            userDto.setRoleId(user.getRole().getId());
            userDto.setRoleName(user.getRole().getName());
           // arr.add(userDto);

        return userDto;


    }

通过Id查找未通过使用用户对象获取角色详细信息。未进行初始化。当我用来从用户对象获取用户ID时,我无法获取角色详细信息。 userDto.setRoleId(user.getRole()的getId());具有空值

2 个答案:

答案 0 :(得分:0)

User实体中,您已将Role集合配置为懒惰加载。因此,当您第一次调用user.getRole()时,您将获得一个代理对象。如果要在代理对象上调用需要获取数据的方法,则应初始化代理。有一种技术可以初始化它。请尝试以下方法:

在以下代码行之前

userDto.setRoleId(user.getRole().getId());

添加以下内容;

user.getRole().size();

答案 1 :(得分:0)

对于您知道需要特定关联的情况,通常建议您指定关联是加入提取的。

如果关联是可选的,您可以使用以下内容:

FROM User u LEFT JOIN FETCH u.role WHERE u.id = :userId

如果关联不是可选的,您可以通过指定内连接来改进上述内容,如:

FROM User u JOIN FETCH u.role WHERE u.id = :userId

使用Spring数据,您可以在方法上使用@Query注释,并指定上面的JPQL / HQL以满足您的需要。