【问题标题】:AliasToBeanResultTransformer(MyDTO.class) fails to instantiate MyDTOAliasToBeanResultTransformer(MyDTO.class) 无法实例化 MyDTO
【发布时间】:2012-03-25 05:41:35
【问题描述】:

我想要一个 Criteria 查询来使用 AliasToBeanResultTransformer 实例化 DTO 类。目标是生成一个带有 ID 的轻量级分页列表,用于主页的进一步操作。这需要报告类型查询。

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

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

public ProfileDTO(Long id, String lastName, String fullName, String email,
        String phone) {
    this(id,fullName);
    this.lastName = lastName;
    this.email = email;
    this.phone = phone;
}

当我使用结果行手动构造 ProfileDTO 对象时,查询有效

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

我的解决方法运行良好,但似乎没有必要。我做错了什么?

【问题讨论】:

    标签: hibernate


    【解决方案1】:

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

    您的 DTO 似乎对元组的所有元素都有一个设置器,除了 lastName。也许这就是问题所在。

    也就是说,您的代码简单、易于维护且可重构。它不能用 AliasToBeanResultTransformer 重构。就像您一样,我通常更喜欢自己实例化我的 DTO。

    【讨论】:

    • 谢谢 JB。我将不再担心并拥抱DIY。正如你所说,它可以正常工作。
    • 你能举个例子吗?
    【解决方案2】:

    试试这个:

    public List<ProfileDTO> getProfiles() throws HibernateException 
    {       
        try { 
    
            session = HibernateUtil.getSessionFactory().openSession();
    
            Criteria criteria = session.createCriteria(Profile.class);
    
            criteria.createAlias("personalData", "pd");
            criteria.createAlias("emails", "e");
            criteria.createAlias("telephones", "t");
    
            criteria.setProjection(Projections.projectionList()
                        .add(Projections.property("id").as( "id"))
                        .add(Projections.property("pd.lastName").as("lastName"))
                        .add(Projections.property("pd.fullName").as("fullName"))
                        .add(Projections.property("e.emailAddress").as("email"))
                        .add(Projections.property("t.phoneNumber").as("phone"))
            );
    
            criteria.setResultTransformer(Transformers.aliasToBean(ProfileDTO.class));
    
            return (List<ProfileDTO>)criteria.list();
    
        } catch (HibernateException he){
            he.printStackTrace();
            throw he; 
        } finally {
             if(session.isOpen()){
                 session.close();
             }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-28
      • 2022-01-03
      • 2016-06-09
      • 2020-10-26
      • 2016-11-22
      • 2016-02-29
      • 2017-01-29
      • 2020-08-30
      • 2011-02-28
      相关资源
      最近更新 更多