Search code examples
javamysqlhibernatejpacriteria

hibernate criteria projection get columns of join tables


I have this entities:

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

}

I want to get only idUser, user and idPerfil; the I use:

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

for the entity user works fine, I get the values of the attributes especify, but from idPerfil I get all values attributes and I only want the idPerfil. How can I do the query?


Solution

  • Try adding an alias for idPerfil:

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

    and then reference it in your projection:

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

    Then remove the transformer:

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

    and change the result to:

    List list = criteria.list();

    Each element in the list will be an Object[] array:

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