简体   繁体   English

Hibernate hql/criteria 结果包含集合

[英]Hibernate hql/criteria result contains collection

I have two 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
}

I am trying to get this DTO:我正在尝试获取此 DTO:

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

But I can't find the right solution.但我找不到正确的解决方案。 I wrote next criteria - find user with photos by login:我写了下一个标准 - 通过登录找到有照片的用户:

getCurrentSession().createCriteria(User.class, "user")
    .createAlias("user.photos", "photos")
    .setProjection(getUserProjection())
    .add(Restrictions.eq("user.login",login))
    .setResultTransformer(Transformers.aliasToBean(UserDTO.class))
    .list();
 // projection   
    getUserProjection() {
        return Projections.projectionList()
            .add(Projections.property("user.id"), "id")
            .add(Projections.property("photos"), "url");
    }

Also tried with HQL:还尝试使用 HQL:

getCurrentSession()
    .createQuery("select u.id, p.url " +
        " from User u inner join u.photos p " +
        " where u.login LIKE :login")
    .setString("login", login)
    .list();

But returned result is List<Object[]> type but I need List<UserDTO> .但返回的结果是List<Object[]>类型,但我需要List<UserDTO>


Update:更新:

HTTP Status 500 - Request processing failed; HTTP 状态 500 - 请求处理失败; nested exception is org.springframework.orm.hibernate4.HibernateSystemException: IllegalArgumentException occurred while calling setter for property [com.memories.dto.UserDTO.photos(expected type = java.util.List)];嵌套异常是 org.springframework.orm.hibernate4.HibernateSystemException: IllegalArgumentException 在为属性 [com.memories.dto.UserDTO.photos(expected type = java.util.List)] 调用 setter 时发生; target = [com.memories.dto.UserDTO.photos@4e162869], property value = [ https://i1.sndcdn.com/artworks-000031744302-0730nk-t500x500.jpg] setter of com.memories.dto.UserDTO.photos;目标 = [com.memories.dto.UserDTO.photos@4e162869],属性值 = [ https://i1.sndcdn.com/artworks-000031744302-0730nk-t500x500.jpg] com.memories.dto.UserDTO 的设置者。相片; nested exception is IllegalArgumentException occurred while calling setter for property [com.memories.dto.UserDTO.photos] (expected type = java.util.List)];嵌套异常是 IllegalArgumentException 在为属性 [com.memories.dto.UserDTO.photos] 调用 setter 时发生(预期类型 = java.util.List)]; target = [com.memories.dto.UserDTO.photos@4e162869], property value = [ https://i1.sndcdn.com/artworks-000031744302-0730nk-t500x500.jpg]目标 = [com.memories.dto.UserDTO.photos@4e162869],属性值 = [ https://i1.sndcdn.com/artworks-000031744302-0730nk-t500x500.jpg]

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.我认为使用上面的代码,您所要做的就是将结果直接分配给 List,它应该可以轻松工作。 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> :接下来,我们像这样在实体 User 方法中声明,将List<Photo>转换为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:最后一步是硬编码创建 DTO bean:

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.而且,当然,您必须具有UserDTO(Long id, List<String> urls)构造函数。 If anybody will find better solution, pls write here :)如果有人会找到更好的解决方案,请在这里写:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM