AliasToBeanResultTransformer(MyDTO.class)无法实例化MyDTO [英] AliasToBeanResultTransformer(MyDTO.class) fails to instantiate MyDTO

查看:126
本文介绍了AliasToBeanResultTransformer(MyDTO.class)无法实例化MyDTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望Criteria查询使用AliasToBeanResultTransformer实例化DTO类。我们的目标是制作一个带有ID的轻量级分页列表,以便为主页提供进一步的操作。这需要一个报告类型的查询。

  Criteria crit = session.createCriteria(Profile.class); 

crit.createAlias(personalData,pd);
crit.createAlias(emails,e);
crit.createAlias(电话,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(新的AliasToBeanResultTransformer(ProfileDTO.class));
profiles = crit.list();

这无法实例化我的DTO类。 ProfileDTO有一个匹配的构造函数:

  public ProfileDTO(Long id,String lastName,String fullName,String email,
String电话){
this(id,fullName);
this.lastName = lastName;
this.email =电子邮件;
this.phone = phone;





$ b

当我使用结果行手动构造ProfileDTO对象时, p>

  List< Object []> rows = crit.list(); 

for(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);
}

我的解决方法工作正常,但似乎没有必要。我在做什么错了?

解决方案

AliasToBeanResultTransformer 使用setter填充DTO。如果您想使用构造函数来创建您的bean实例,您需要使用 AliasToBeanConstructorResultTransformer



您的DTO似乎对元组的所有元素都有setter,除了lastName。也许这是问题。



也就是说,您的代码很简单,易于维护和重构。它不会与AliasToBeanResultTransformer重构。我一般喜欢自己实例化我的DTO,就像你一样。

I would like a Criteria query to instantiate a DTO class using an AliasToBeanResultTransformer. The goal is to produce a light weight paged list with IDs for further action for a home page. This calls for a 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 to instantiate my DTO class. ProfileDTO has a 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 the query works when I construct ProfileDTO objects manually with the result rows

        List<Object[]> rows = crit.list();

        for ( 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 is working fine, but it seems unnecessary. What am I doing wrong?

解决方案

The AliasToBeanResultTransformer uses setters to populate the DTO. If you want to use a constructor, to create your bean instance, you need to use an AliasToBeanConstructorResultTransformer.

Your DTO seems to have a setter for all the elements of the tuple, except for lastName. Maybe this is the problem.

That said, your code as is is simple, easy to maintain, and refactorable. It wouldn't be refactorable with an AliasToBeanResultTransformer. I generally prefer to instantiate my DTOs myself, just as you did.

这篇关于AliasToBeanResultTransformer(MyDTO.class)无法实例化MyDTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆