hibernate - AliasToBeanResultTransformer(MyDTO.class) fails to instantiate MyDTO -
i criteria query instantiate dto class using aliastobeanresulttransformer. goal produce light weight paged list ids further action home page. calls reporting type query.
criteria crit = session.createcriteria(profile.class); crit.createalias("personaldata", "pd"); crit.createalias("emails", "e"); crit.createalias("telephones", "t"); projectionlist properties = projections.projectionlist(); properties.add(projections.property("id").as( "id")); properties.add(projections.property("pd.lastname").as("lastname")); properties.add(projections.property("pd.fullname").as("fullname")); properties.add(projections.property("e.emailaddress").as("email")); properties.add(projections.property("t.phonenumber").as("phone")); crit.setprojection(properties); crit.setresulttransformer(new aliastobeanresulttransformer(profiledto.class)); profiles = crit.list();
this fails instantiate dto class. profiledto has matching constructor:
public profiledto(long id, string lastname, string fullname, string email, string phone) { this(id,fullname); this.lastname = lastname; this.email = email; this.phone = phone; }
and query works when construct profiledto objects manually result rows
list<object[]> rows = crit.list(); ( object[] row: rows ) { profiledto dto = new profiledto(); dto.setid((long)row[0]); dto.setlastname((string)row[1]); dto.setfullname((string)row[2]); dto.setemail((string)row[3]); dto.setphone((string)row[4]); profiles.add(dto); }
my workaround working fine, seems unnecessary. doing wrong?
the aliastobeanresulttransformer uses setters populate dto. if want use constructor, create bean instance, need use aliastobeanconstructorresulttransformer.
your dto seems have setter elements of tuple, except lastname. maybe problem.
that said, code is simple, easy maintain, , refactorable. wouldn't refactorable aliastobeanresulttransformer. prefer instantiate dtos myself, did.
Comments
Post a Comment