-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problems with aliasToBean Transformer
PostPosted: Tue Oct 10, 2006 9:54 am 
Regular
Regular

Joined: Mon Mar 06, 2006 6:18 am
Posts: 95
Location: Bern, Switzerland
Hibernate version: 3.2 CR5

Hi all

i'm trying to use a Result Transformer, like described in:
http://blog.hibernate.org/cgi-bin/blosx ... 006/03/17/


Here is my hibernate query:

Code:
public List<AngebotContainer> searchAngeboteNEW(final JuausAngebot angebot,
         final List<Integer> allowedOrganisationIdList) {
      ANGEBOT_CONSTRAINT.validate(angebot);

      HibernateTemplate hibernateTemplate = new HibernateTemplate(getSessionFactory());
      return (List<AngebotContainer>) hibernateTemplate.execute(new HibernateCallback() {
         public Object doInHibernate(Session session) throws HibernateException {
            Criteria criteria = session.createCriteria(JuausAngebot.class);
            criteria.setResultTransformer(Transformers.aliasToBean(AngebotContainer.class));
            
            
            criteria.createAlias("angebotsArt", "angebotsArt");
            criteria.createAlias("organisation", "organisation");

            ProjectionList pl = Projections.projectionList();
            pl.add(Projections.property("angebotsArt"), "angebotsArt");
            pl.add(Projections.property("organisation"), "organisation");
            pl.add(Projections.property("nutzergruppe"), "nutzergruppe");
            pl.add(Projections.property("mutUser"), "mutUser");
            pl.add(Projections.property("mutDatum"), "mutDatum");
            criteria.setProjection(pl);
            
            if (angebot.getAngebotsArt() != null) {
               criteria.add(Restrictions.eq("angebotsArt.id", angebot.getAngebotsArt().getId()));
            }

//more critera

            // don't return the deleted angebote
            criteria.add(Restrictions.ne("status", AngebotStatus.GELOESCHT));
            
            return criteria.list();
         }
      });
   }


In my Manager i get the list which contains Objects Arrays. Then i'll try to cast it to a AngebotContainer and i get a class cast exception. But the the example it seems like its done the same way:
Code:
StudentDTO dto = (StudentDTO)resultWithAliasedBean.get(0); 



Here is my ManagerCode which causes the exception:

Code:
List<AngebotContainer> agbListNEW = angebotDao.searchAngeboteNEW(angebotCriteria, orgIds);
      AngebotContainer test = (AngebotContainer)agbListNEW.get(0);


What did i understand wrong?

thanks
angela


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 10:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
what is actually returned ?

and try and set the resulttransformer just before you do the list.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 10:37 am 
Regular
Regular

Joined: Mon Mar 06, 2006 6:18 am
Posts: 95
Location: Bern, Switzerland
max wrote:
what is actually returned ?

and try and set the resulttransformer just before you do the list.


hi max

yes it working if i set the resulttransformer just before the .list() method:

Code:
return criteria.setResultTransformer(
                  Transformers.aliasToBean(AngebotContainer.class)).list();


Thanks a lot!!! Why is it like that?

Another question, if i add the propery status to the Projection i get an error:
Code:
pl.add(Projections.property("status"), "status");

2006-10-10 16:30:13,292 ERROR ORA-00904: "Y5_": invalid identifier
in (JDBCExceptionReporter.java:72)


In the class JuausAngebot and AngebotContainer status is of type "AngebotStatus":

Code:
public class AngebotStatus {
   /**
    * Status 100: Soll-Daten erfassen
    */
   public static final AngebotStatus SOLL_DATEN_ERFASSEN = new AngebotStatus(new Integer(100));

   /**
    * Status 110: Angebot zur Bewilligung freigegeben
    */
   public static final AngebotStatus ZUR_BEWILLIGUNG_FREIGEGEBEN = new AngebotStatus(new Integer(
         110));

   
   private static final Map instancesByValue;

   private Integer value;

   static {
      instancesByValue = new HashMap();
      instancesByValue.put(SOLL_DATEN_ERFASSEN.getValue(), SOLL_DATEN_ERFASSEN);
      instancesByValue.put(ZUR_BEWILLIGUNG_FREIGEGEBEN.getValue(), ZUR_BEWILLIGUNG_FREIGEGEBEN);
   }

   /**
    * Creates a new <code>AngebotStatus</code> object.
    *
    * @param value
    */
   private AngebotStatus(final Integer value) {
      this.value = value;
   }

   /**
    * @param value
    * @return KursStatus
    */
   public static AngebotStatus getInstanceByValue(final Integer value) {
      if ((value == null) || (!instancesByValue.containsKey(value))) {
         return null;
      }
      else {
         return (AngebotStatus) instancesByValue.get(value);
      }
   }

   /**
    * @return Integer
    */
   public Integer getValue() {
      return value;
   }

   /**
    * @return int
    */
   public int intValue() {
      return value.intValue();
   }

   /**
    * @return String
    */
   public String toString() {
      return value.toString();
   }

   /**
    * @return Object
    * @throws ObjectStreamException
    */
   protected Object readResolve() throws ObjectStreamException {
      return toString();
   }

   /**
    * @param o
    */
   public boolean equals(Object o) {
      if (o instanceof AngebotStatus) {
         AngebotStatus ob = (AngebotStatus) o;
         return value.equals(ob.value);
      }
      return false;
   }
}


any idea?

angela


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 12:06 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
it works because in one of the steps a new criteria is actually returned and this new one doesn't have the transformer associated....maybe we should actually do that..hmm...

regarding your other question, then no, I don't know why it does that.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 11, 2006 2:00 am 
Regular
Regular

Joined: Mon Mar 06, 2006 6:18 am
Posts: 95
Location: Bern, Switzerland
max wrote:
regarding your other question, then no, I don't know why it does that.

ok so i'll keep trying it....

something else..is it possible to add a detached criteria?? one property of my DTO (AngebotContainer) specifies the number of Kurse. For that i added a Detached Criteria:

Code:
DetachedCriteria anzahlKurseCriteria = DetachedCriteria.forClass(Kurs.class);
            anzahlKurseCriteria.setProjection(Projections.count("id"));
            anzahlKurseCriteria.add(Restrictions.eq("angebot.id", angebot.getId()));
            anzahlKurseCriteria.add(Restrictions.ne("kursStatus", KursStatus.GELOESCHT));
            anzahlKurseCriteria.add(Restrictions.isNull("parent"));


How can i add that to my projection now? In order it fills this count value into the "anzahlKurse" property of my Angebot Container???

Angela


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.