-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: PropertyNotFoundException using AliasToBeanResultTransformer
PostPosted: Thu Apr 07, 2016 6:49 pm 
Newbie

Joined: Thu Apr 07, 2016 5:05 pm
Posts: 2
Exception: org.hibernate.PropertyNotFoundException: Could not find setter for IEN on class ...MailLists

Code resulting in exception:
Code:
query.setResultTransformer(new AliasToBeanResultTransformer(MailLists.class));


Class generated by hbm2java:
Code:
public class MailLists  implements java.io.Serializable {
     private int ien;

    @Column(name="IEN", unique=true, nullable=false)
    public int getIen() {
        return this.ien;
    }
   
    public void setIen(int ien) {
        this.ien = ien;
    }


It seems like the problem is because the database table column name is IEN and the transformer is looking for setIEN(). I cannot change the generation of the MailLists class, so what other options are available to populate MailLists?

Thanks.


Top
 Profile  
 
 Post subject: Re: PropertyNotFoundException using AliasToBeanResultTransformer
PostPosted: Fri Apr 08, 2016 1:05 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
Hibernate uses the fields (with Reflection), not the setters, so you have two options:

1. You change the field name to:

Code:
private int IEN;


2. You add an alias to your query:

Code:
List<MailLists> dtos = session.createSQLQuery(
    "SELECT ml.IEN as \"ien\"" +
    "FROM mail_list ml")
.setResultTransformer( Transformers.aliasToBean( MailLists.class ) )
.list();

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: PropertyNotFoundException using AliasToBeanResultTransformer
PostPosted: Fri Apr 08, 2016 2:55 pm 
Newbie

Joined: Thu Apr 07, 2016 5:05 pm
Posts: 2
Unfortunately, option 1 is out because the class is generated. Option 2 does not work either because the query is actually a procedure call. But, I was able to add the alias to the stored procedure query and all is good now.

Thanks.


Top
 Profile  
 
 Post subject: Re: PropertyNotFoundException using AliasToBeanResultTransformer
PostPosted: Thu May 26, 2016 6:14 am 
Newbie

Joined: Wed May 25, 2016 12:57 pm
Posts: 2
AliasToBeanResultTransformer tries set a property to the MailLists using a setter or a field (AliasToBeanResultTransformer#L90) . So, this should work too (at least with Hibernate 5)
Code:
public class MailLists {
     
    private int ien;

    @Column(name="IEN", unique=true, nullable=false)
    public int getIEN() {
        return this.ien;
    }
   
    public void setIEN(int ien) {
        this.ien = ien;
    }

}


Using such setters is undesirable, of course. To solve the issue a custom result transformer can be used: mapping Hibernate query results to custom class?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.