开发者

Transformers for SQL to return non entity pojo class

hi i am trying to return non entity class of the resulting query using Transformers in hibernate.

My pojo class is

Hibernate code is

    public class TestPojo {

    private String id=null;
    private String companyname=null;
    private String fullname=null;
    private String empid=null;
    private String dateallocated=null;
    public TestPojo() {

    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getCompanyname() {
        re开发者_运维百科turn companyname;
    }
    public void setCompanyname(String companyname) {
        this.companyname = companyname;
    }
    public String getFullname() {
        return fullname;
    }
    public void setFullname(String fullname) {
        this.fullname = fullname;
    }
    public String getEmpid() {
        return empid;
    }
    public void setEmpid(String empid) {
        this.empid = empid;
    }
    public String getDateallocated() {
        return dateallocated;
    }
    public void setDateallocated(String dateallocated) {
        this.dateallocated = dateallocated;
    }




        SQLQuery query = session.createSQLQuery("select t.id as id,t.companyname      as           companyname,e.fullname as fullname,e.empid as empid,ca.dateallocated as    dateallocated from   bw_tempclientdetails t,  bw_employee_details e, bw_clientallocation   ca   where  e.empid=ca.empid and  ca.companyname=t.companyname");

         query.addScalar("id");    
         query.addScalar("companyname");
         query.addScalar("fullname");
         query.addScalar("empid");
         query.addScalar("dateallocated");


query.setResultTransformer(Transformers.aliasToBean(TestPojo.class));
List<TestPojo> list=query.list();

But when i run my application i get this error. org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of org.bluewhale.model.TestPojo.id

enter code here


As mentioned in your comments:-

query.addScalar("id",Hibernate.STRING);
query.addScalar("companyname",Hibernate.STRING); 
query.addScalar("fullname",Hibernate.STRING); 
query.addScalar("empid",Hibernate.STRING); 
query.addScalar("dateallocated",Hibernate.STRING);
query.addScalar("status",Hibernate.STRING); 
query.setResultTransformer(Transformers.aliasToBean(TestPojo.class)); 
list=query.list();

Explanation :

To explain in general Hibernate chooses the most suitable the way in which the data can be represented. So I assume that the IDs are stored in number format. Hence Hibernate considers them as Long. The aliasToBean method calls the default constructor to make the TestPojo object. It then searches for the setter methods for its self deduced types. In this case probably it searches for the method with signature

  public void setId(Long id)

this call goes to the overloaded method public void setId(String id)

Since Long is not String you get a IllegalArgumentException.
What you did in the solution is explicitly define the values to be taken as type String hence now the setter method used by Hibernate becomes public void setId(String id) therefore solving your problem.

Please Note:- If you are to use addScalar() you need to use it for all the data members needed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜