【问题标题】:hibernate criteria projection get columns of join tables休眠标准投影获取连接表的列
【发布时间】:2014-11-07 22:09:13
【问题描述】:

我有这个实体:

public class User
{
    private Integer idUser;
    private String user;
    private String password;
    private Perfil perfil;

    Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "idUser", unique = true, nullable = false)
    public Integer getIdUser()
    {
        return this.idUser;
    }

    public void setIdUser(Integer idUser)
    {
        this.idUser = idUser;
    }

    @Column(name = "user", nullable = false)
    public String getUser()
    {
        return this.user;
    }

    public void setUser(String user)
    {
        this.user = user;
    }

    @Column(name = "password", nullable = false)
    public String getPassword()
    {
        return this.password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    @OneToOne
    @JoinColumn(name = "idPerfil", nullable = false)
    public Perfil getIdPerfil()
    {
        return idPerfil;
    }

    public void setIdPerfil(Perfil idPerfil)
    {
        this.idPerfil = idPerfil;
    }
}

public class Perfil
{
    private Integer idPerfil;
    private String perfil;
    private String description;

    Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "idPerfil", unique = true, nullable = false)
    public Integer getIdPerfil()
    {
        return this.idPerfil;
    }

    public void setIdPerfil(Integer idPerfil)
    {
        this.idPerfil = idPerfil;
    }

    @Column(name = "description", nullable = false)
    public String getDescription()
    {
        return this.description;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

}

我只想获取 idUser、user 和 idPerfil;我用的:

Session session = HibernateUtil.openSession();

        try
        {
            Criteria criteria = session.createCriteria(User.class);
            ProjectionList proList = Projections.projectionList();
            proList.add(Projections.property("idUser"), "idUser");
            proList.add(Projections.property("user"), "user");
            proList.add(Projections.property("idPerfil"), "idPerfil");
            criteria.setProjection(proList);
            criteria.setFetchMode("idPerfil.idPerfil", org.hibernate.FetchMode.JOIN);
            criteria.setResultTransformer(Transformers.aliasToBean(User.class));
            List<User> list = criteria.list();
            return list;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            session.close();
        }

对于实体用户工作正常,我得到属性 especify 的值,但从 idPerfil 我得到所有值属性,我只想要 idPerfil。如何查询?

【问题讨论】:

    标签: java mysql hibernate jpa criteria


    【解决方案1】:

    尝试为 idPerfil 添加别名:

    criteria.createAlias("idPerfil", "idp");
    

    然后在你的投影中引用它:

    proList.add(Projections.property("idp.idPerfil"), "idPerfil");
    

    然后拆下变压器:

    criteria.setResultTransformer(Transformers.aliasToBean(User.class));
    

    并将结果更改为:

    列表列表=criteria.list();

    列表中的每个元素都是一个 Object[] 数组:

    for(Object data : list) {
        Object[] projection = (Object[]) data;
        Integer idUser = projection[0];
        String user = projection[1];
        Integer idPerfil= projection[3];
        ...
    }
    

    【讨论】:

    • 我试了一下,但是使用criteria.setResultTransformer(Transformers.aliasToBean(User.class));时出错。
    • org.hibernate.property.BasicPropertyAccessor$BasicSetter.set HHH000091:预期类型:com.entity.Perfil,实际值:java.lang.Integer错误org.hibernate.PropertyAccessException:调用setter时发生IllegalArgumentException com.entity.User.idPerfil org.hibernate.PropertyAccessException:调用 com.User.idPerfil 的设置器时发生 IllegalArgumentException。如果我不使用 criteria.setResultTransformer(Transformers.aliasToBean(User.class)); idPerfil 看起来像 Integer 而不是 object
    • 好的,谢谢,但没有办法将其转换为用户?因为我更喜欢像实体一样工作
    • 投影在仅选择实体的一部分时很有用。您还可以将选定的列映射到 DTO。对于获取不需要投影的实体
    猜你喜欢
    • 2011-04-11
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    相关资源
    最近更新 更多