Hibernate hql/criteria result contains collection

左心房为你撑大大i 提交于 2019-12-02 02:44:13

I think with the above code all you have to do it is to assign the resultant directly to List and it should work like a breeze. something like this.

List<User> userList = yourCriteria.list();

So, i found not the best solution, but...

Firstly, need to get list of entities User:

List<User> list = getCurrentSession()
                .createQuery("from USer as u where u.login LIKE :login")
                .setString("login", login)
                .list();

Next, we declare in entity User method like this one, which convert List<Photo> to List<String>:

public List<String> getPhotosUrls() {
    List<String> urls = new ArrayList<String>(photos.size());
    for (Photo p : photos)
        urls.add(p.getUrl()));
    return urls;
}

And last step is hard-coded creating DTO beans:

List<UserDTO> users = new ArrayList<UserDTO>(list.size());
for (User u : list) {
    users.add(new UserDTO(u.id, u.getPhotosUrls));

And, of course, you must have UserDTO(Long id, List<String> urls) constructor. If anybody will find better solution, pls write here :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!