java - Hibernate hql/criteria result contains collection -
i have 2 entities:
public class photo { long id; string url; @manytoone @joincolumn(name ="user_id") user user; // other fields , getters/setters }
and second:
public class user { long id; @onetomany(mappedby = "user") private collection<photo> photos; // other fields , getters/setters }
i trying dto:
public class userdto { long id; list<string> photosurls; }
but can't find right solution. wrote next criteria - find user photos 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 hql:
getcurrentsession() .createquery("select u.id, p.url " + " user u inner join u.photos p " + " u.login :login") .setstring("login", login) .list();
but returned result list<object[]>
type need list<userdto>
.
update:
http status 500 - request processing failed; nested exception org.springframework.orm.hibernate4.hibernatesystemexception: illegalargumentexception occurred while calling setter property [com.memories.dto.userdto.photos(expected type = java.util.list)]; 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; nested exception illegalargumentexception occurred while calling setter property [com.memories.dto.userdto.photos] (expected type = java.util.list)]; target = [com.memories.dto.userdto.photos@4e162869], property value = [https://i1.sndcdn.com/artworks-000031744302-0730nk-t500x500.jpg]
i think above code have assign resultant directly list , should work breeze. this.
list<user> userlist = yourcriteria.list();
Comments
Post a Comment