1

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?

1 Answer 1

2

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];
    ...
}
4
  • I try it, but I got error when I use criteria.setResultTransformer(Transformers.aliasToBean(User.class));. Nov 10, 2014 at 21:14
  • org.hibernate.property.BasicPropertyAccessor$BasicSetter.set HHH000091: Expected type: com.entity.Perfil, actual value: java.lang.Integer error org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.entity.User.idPerfil org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.User.idPerfil. if I not use criteria.setResultTransformer(Transformers.aliasToBean(User.class)); idPerfil appears like Integer and not like object Nov 10, 2014 at 21:15
  • ok thanks, but there isn´t way to transform it to user? because I prefer work it like entities Nov 10, 2014 at 22:46
  • Projections are useful when selecting only parts of an entity. You can also map the selected columns to a DTO. For fetching entities you don't need projections Nov 11, 2014 at 5:21

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.