Hibernate aliasToBean with field collection

35 views Asked by At

I have next entities:

public class Photo {
   Long id;
   String url;
   @ManyToOne
   @JoinColumn(name ="user_id")
   User user;
   // other fields and getters/setters
}

And second:

public class User {
   Long id;
   @OneToMany(mappedBy = "user")
   private Collection<Photo> photos;
   // other fields and getters/setters
}

DTO:

public class UserDTO {
    Long id;
    List<String> photosUrls;
}

And HQL query:

 select u.id, p.url from User as u inner join u.photos as p
    where u.id = :id

In result i want my UserDTO object, which i will use in rest(json). But my query like this throws exceptions, with error:

HibernateSystemException: IllegalArgumentException occurred while calling setter for property [com.dto.UserDTO.photosUrls (expected type = java.util.List)]; target = [com.dto.UserDTO@7650a5f3], property value = [some url]

I can't find right solution to get result objects list like:

List<UserDTO> result;

I read another answer, but my result of List will contain dublicates of users id and unique photo url.

user_id     url
   1       http1
   1       http2
   3       http3
   3       http4

This question more explained that this question. And, also, there are no right answer.

0

There are 0 answers