0

I have two tables and trying to get data with inner join using hibernate with spring. And i will get json using ModelMapper and get duplicate record also. So, please suggest me how to get unique record with inner join query using hibernate.

I have Two Tables:

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;
    }   
}
  • Can you show your query, so we can help determine why you're getting duplicate records? Also, what does `ModelMapper` have to do with with applying an inner join? – Naros Oct 07 '16 at 15:20
  • http://stackoverflow.com/questions/18753245/one-to-many-relationship-gets-duplicate-objects-whithout-using-distinct-why – Alan Hay Oct 07 '16 at 17:47
  • SQL Query: SELECT * FROM user inner join userrole on (user.id=userrole.userid) AND ModelMapper do the convert JPA entity to JSON Object. But in spring we have an option of jackson for JSON conversation so....! – user3024220 Oct 18 '16 at 02:33

0 Answers0