-->
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.  [ 9 posts ] 
Author Message
 Post subject: Newbie: Transform resultset
PostPosted: Sun Sep 30, 2007 3:27 pm 
Newbie

Joined: Wed Sep 19, 2007 9:13 am
Posts: 2
Hi,

I'm trying to return a list of a bean that contains a subset of the original entity, but I'm doing this wrong somehow I guess.

Normal way to return a complete list (User is my entity).
Code:
    public List<User> list() {
        return (List<User>) session.createCriteria(User.class).list();
    }


Now I instead wanted to return a list of AdminListUser instead, I thought I just could do this:
Code:
    public List<AdminListUser> adminList() {
        return (List<AdminListUser>) session.createCriteria(User.class).setResultTransformer(
                Transformers.aliasToBean(AdminListUser.class)).list();
    }


but then I get an exception.

Hibernate version:
3.2.5.ga

Full stack trace of any exception that occurs:
Caused by: org.hibernate.PropertyNotFoundException: Could not find setter for this on class nu.localhost.anopo.logic.beans.AdminListUser
at org.hibernate.property.ChainedPropertyAccessor.getSetter(ChainedPropertyAccessor.java:44)
at org.hibernate.transform.AliasToBeanResultTransformer.transformTuple(AliasToBeanResultTransformer.java:57)
at org.hibernate.loader.criteria.CriteriaLoader.getResultColumnOrRow(CriteriaLoader.java:115)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:606)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at nu.localhost.anopo.logic.dao.impl.UserDaoImpl.adminList(UserDaoImpl.java:68)

Name and version of the database you are using:
PostgreSQL 8.2.4

The generated SQL (show_sql=true):
Hibernate: select this_.user_id as user1_1_0_, this_.name as name1_0_, this_.password as password1_0_, this_.username as username1_0_, this_.enabled as enabled1_0_, this_.email as email1_0_, this_.expires as expires1_0_ from users this_


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 9:20 pm 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
From javadoc of AliasToBeanResultTransformer:

Result transformer that allows to transform a result to a user specified class which will be populated via setter methods or fields matching the alias names.

What I think happens in your case, is that there is defined one alias for table User -> this.

Result transformer tries to call AdminListUser.setThis(User user) method in this case. I guess.

Try to use the aliasToBeanConstructor or something else.

I found some links:
http://blog.hibernate.org/Bloggers/Everyone/2006/03/17

Regrads,
Pavol


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 02, 2007 4:58 pm 
Newbie

Joined: Wed Sep 19, 2007 9:13 am
Posts: 2
pavol.zibrita wrote:
What I think happens in your case, is that there is defined one alias for table User -> this.

Result transformer tries to call AdminListUser.setThis(User user) method in this case. I guess.


Yes, doing that I no longer get an exception, however, user is always null when called here.

Do I need to make a create a Projection? Can I create a project from User.class? Otherwise I still have to hardcode how to user class looks like.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 02, 2007 6:18 pm 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
Hm, I really don't know, never tried this. I'm using hibernate more as JPA implementation.

But try to define the alias by yourself and change the method accordingly. Also you can try the projections, I really have no experience with it. I would also like to know, what helped :-).

Regards,
Pavol


Top
 Profile  
 
 Post subject: Re: Newbie: Transform resultset
PostPosted: Wed Jun 17, 2009 7:53 am 
Newbie

Joined: Wed Jun 17, 2009 7:45 am
Posts: 1
Bumping an old post, but when I googled "Could not find setter for this" I got only 1 match - this post :)

Anyway, I have the exact same problem, does anyone know what is going on here?


Top
 Profile  
 
 Post subject: Re: Newbie: Transform resultset
PostPosted: Thu Jun 18, 2009 5:20 am 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
Hi there.

Can you put here your code here (be brief but include all necessary things) and the intention why your are doing this?

Pavol


Top
 Profile  
 
 Post subject: Re: Newbie: Transform resultset
PostPosted: Tue Jul 14, 2009 3:29 am 
Beginner
Beginner

Joined: Thu Sep 18, 2008 5:18 am
Posts: 28
Hi, have the same problem here ...
error ..
Code:
2009-07-14 08:16:29,625 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - closing statement
2009-07-14 08:16:29,625 [main] DEBUG org.hibernate.engine.StatefulPersistenceContext - initializing non-lazy collections
org.hibernate.PropertyNotFoundException: Could not find setter for FORCE on class hibernate.model.Member
   at org.hibernate.property.ChainedPropertyAccessor.getSetter(ChainedPropertyAccessor.java:44)
   at org.hibernate.transform.AliasToBeanResultTransformer.transformTuple(AliasToBeanResultTransformer.java:57)
   at org.hibernate.hql.HolderInstantiator.instantiate(HolderInstantiator.java:69)
   at org.hibernate.loader.custom.CustomLoader.getResultList(CustomLoader.java:330)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
   at org.hibernate.loader.Loader.list(Loader.java:2099)
   at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
   at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
   at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
   at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
   at hibernate.dao.impl.MemberDAO.findAllBySQL(MemberDAO.java:166)
   at hibernate.service.MemQualServiceImpl.getAllMembers(MemQualServiceImpl.java:37)
   at hibernate.apptest.HibTest01.init(HibTest01.java:81)
   at hibernate.apptest.HibTest01.main(HibTest01.java:47)
2009-07-14 08:16:29,640 [main] ERROR hibernate.dao.impl.MemberDAO - find all by SQL failed
org.hibernate.PropertyNotFoundException: Could not find setter for FORCE on class hibernate.model.Member
   at org.hibernate.property.ChainedPropertyAccessor.getSetter(ChainedPropertyAccessor.java:44)
   at org.hibernate.transform.AliasToBeanResultTransformer.transformTuple(AliasToBeanResultTransformer.java:57)
   at org.hibernate.hql.HolderInstantiator.instantiate(HolderInstantiator.java:69)
   at org.hibernate.loader.custom.CustomLoader.getResultList(CustomLoader.java:330)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
   at org.hibernate.loader.Loader.list(Loader.java:2099)
   at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
   at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
   at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
   at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
   at hibernate.dao.impl.MemberDAO.findAllBySQL(MemberDAO.java:166)
   at hibernate.service.MemQualServiceImpl.getAllMembers(MemQualServiceImpl.java:37)
   at hibernate.apptest.HibTest01.init(HibTest01.java:81)
   at hibernate.apptest.HibTest01.main(HibTest01.java:47)
2009-07-14 08:16:29,640 [main] DEBUG hibernate.service.MemQualServiceImpl - Failed to initialize Member list
2009-07-14 08:16:29,640 [main] INFO  hibernate.apptest.HibTest01 - About to loop through each member ....

Here's my xml mapping file
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="hibernate.model.Member" table="MEMBERS" schema="LOCNAVY">
   
        <id name="force" type="java.lang.String">
            <column name="FORCE" length="8" />
            <generator class="assigned" />
        </id>
       
        <property name="appCode" type="java.lang.String">
            <column name="APP_CDE" length="2" not-null="true" />
        </property>

        <property name="rankCode" type="java.lang.String">
            <column name="RANK_CDE" length="2" not-null="true" />
        </property>
               
        <property name="surname" type="java.lang.String">
            <column name="SURNAME" length="35" not-null="true" />
        </property>
       
        <property name="initials" type="java.lang.String">
            <column name="INIT" length="5" not-null="true" />
        </property>       
              
     </class>
</hibernate-mapping>

and my POJO
Code:
package hibernate.model;

import sitawc.generic.model.Auditable;

@SuppressWarnings("serial")
public class Member implements java.io.Serializable  {
   
   // Mapped Properties
   private String force;
   private String appCode;
   private String rankCode;
   private String surname;
   private String initials;
   
   /**
    * Default constructor
    */
   
   public Member() {}
   
   /**
    * full constructor
    *
    * @param initials
    * @param surname
    * @param force
    * @param appCode
    * @param rankCode
    */   

   public Member(String appCode, String force,
         String initials, String rankCode, String surname) {
      super();
      this.appCode = appCode;
      this.force = force;
      this.initials = initials;
      this.rankCode = rankCode;
      this.surname = surname;
   }

   /**
    * Property accessors
    */
   
   public String getSurname() {
      return surname;
   }
   public void setSurname(String surname) {
      this.surname = surname;
   }
   public String getForce() {
      return force;
   }
   public void setForce(String force) {
      this.force = force;
   }

   public String getInitials() {
      return initials;
   }

   public void setInitials(String initials) {
      this.initials = initials;
   }

   /**
    * Provide a FullName as a single string
    * @return ForceNo+Surname+Initials
    */
   public String getFullName() {
      return  surname + ", "
            + initials;   
   }

   public String getForceCode() {
      return  force + " "
            + appCode + " "
            + rank;   
   }
   
   public String getAppCode() {
      return appCode;
   }

   public void setAppCode(String appCode) {
      this.appCode = appCode;
   }
   
   public String getRankCode() {
      return rankCode;
   }
   
   public void setRankCode(String rankCode) {
      this.rankCode = rankCode;
   }
}

My createSQLQuery get the list using aliasToBean ...
Code:
queryObject.
   setResultTransformer (
      Transformers.aliasToBean(Member.class))
   .list();


Top
 Profile  
 
 Post subject: Re: Newbie: Transform resultset
PostPosted: Tue Jul 14, 2009 5:09 am 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
Why do you need to do transform, when the Member object is mapped?


Top
 Profile  
 
 Post subject: Re: Newbie: Transform resultset
PostPosted: Tue Jul 14, 2009 9:52 am 
Beginner
Beginner

Joined: Thu Sep 18, 2008 5:18 am
Posts: 28
pavol.zibrita wrote:
Why do you need to do transform, when the Member object is mapped?


1. I'm using a createSQLQuery which returns object[]
2. Member has extra properties which are not mapped (removed them to eliminate confusion)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.