使用Hibernate获取连接表数据

时间:2016-10-07 15:07:37

标签: spring hibernate

我有两个表,并尝试使用带有spring的hibernate来获取内连接数据。 我将使用ModelMapper获取json并获得重复记录。 那么,请建议我如何使用hibernate获取内部联接查询的唯一记录。

我有两张桌子:

Table Name: User
---------------------
id    |    email
---------------------
1     |   m@gmail.com
2     |   e@gmail.com
---------------------

Table Name: Userrole
----------------------------------------
roleid    |    userid     |   rolename
----------------------------------------
1         |     1         |   Admin
2         |     1         |   HR
3         |     2         |   Employee
4         |     2         |   Executive
----------------------------------------

@Entity
@Table(name="user")
public class User{    
     @Id
     @GeneratedValue
     private int id;
     @Column
     private String email;
     @OneToMany
     private List<Userrole> userroles;     
     public void setId(int id){
        this.id = id;
     }
     public void setEmail(String email){
        this.email= email;
     }
     public int getId(){
        return id;
     }
     public String getEmail(){
        return email;
     }
     public List<Userrole> getUserrole(){
        return userroles;
     }
}

@Entity
@Table(name="userrole")
public classs Userrole{
      @Id
      @GeneratorValue
      private int roleid;
      @Column
      private String rolename;
      @ManyToOne
      private User user;

      // setter and getter
}
public List<User> findAall(){

    // Type casting will throw an error

    // This will throw error
    return (List<User>)session.getCurrentSession().createQuery("from User u join u.userroles");

    // this will return List<Object[]>
    return session.getCurrentSession().createQuery("from User u join u.userroles");

    // So i want to convert to this DTO using ModelMapper object
}


public class UserDTO{    
    private int id;
    private String email;
    private List<Userrole> roleusers;

    // setter and getter
}
public class UserroleDTO{
    private int roleid;
    private String rolename;

    //settter and getter
}

public interface UserDao{
    public List<User> findAll();
}

@Repository
public class UserDaoImpl implements UserDao{

     @Override
     public List<User> findAll(){

        // This will throw an error: ClassCastException
        return (List<User>)session.getCurrentSession().createQuery("from User u join u.userroles");

        // This will work perfectly and return List<Object[]>
        return session.getCurrentSession().createQuery("from User u join u.userroles");     
     }
}

@Controller
public class HomeController{

    @Autowired
    private UserDao doa;

    ModelMapper modelMapper = new ModelMapper();

    @RequestMapping(value="/list")
    public List<User> findAll(){
        List<User> list = dao.findAll();
        List<UserDTO> dto = list.stream().map(user -> convertToDto(user)).collect(Collectors.toList());
        return dto;

        // This will return         
        [
            {
                "id":1,
                "email":"m@gmail.com",
                "userroles":[
                    {
                        "roleid":1,
                        "rolename":"Admin"
                    },
                    {
                        "roleid":2,
                        "rolename":"HR"
                    }
                ]
            },
            {
                "id":1,
                "email":"m@gmail.com",
                "userroles":[
                    {
                        "roleid":1,
                        "rolename":"Admin"
                    },
                    {
                        "roleid":2,
                        "rolename":"HR"
                    }
                ]
            },
            {
                "id":2,
                "email":"e@gmail.com",
                "userroles":[
                    {
                        "roleid":3,
                        "rolename":"Employee"
                    },
                    {
                        "roleid":4,
                        "rolename":"Executive"
                    }
                ]
            },
            {
                "id":2,
                "email":"e@gmail.com",
                "userroles":[
                    {
                        "roleid":3,
                        "rolename":"Employee"
                    },
                    {
                        "roleid":4,
                        "rolename":"Executive"
                    }
                ]
            }
        ]

        // It should return 
        [
            {
                "id":1,
                "email":"m@gmail.com",
                "userroles":[
                    {
                        "roleid":1,
                        "rolename":"Admin"
                    },
                    {
                        "roleid":2,
                        "rolename":"HR"
                    }
                ]
            },
            {
                "id":2,
                "email":"e@gmail.com",
                "userroles":[
                    {
                        "roleid":3,
                        "rolename":"Employee"
                    },
                    {
                        "roleid":4,
                        "rolename":"Executive"
                    }
                ]
            }
        ]


    }   
    public UserDTO convertToDto(User user){
        UserDTO dto = modelMapper.map(user,UserDTO.class);
        return dto;
    }   
}

0 个答案:

没有答案