-->
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.  [ 3 posts ] 
Author Message
 Post subject: Using Projections/Transformers to return Map<ID,Entity>
PostPosted: Wed Dec 12, 2007 11:20 pm 
Newbie

Joined: Wed Dec 12, 2007 11:00 pm
Posts: 2
Hello,

I'm using Christian Bauer's example of a GenericDAO as a base class for my own DAOs. If you're not familiar with it, here's the relevant code:

Code:
public abstract class GenericDAO<T,ID extends Serializable> {
   
    public List<T> findByCriteria(Criterion... criterion) {
        Session session =
                ((HibernateEntityManager)getEntityManager()).getSession();
        Criteria crit = session.createCriteria(getEntityBeanType());

        for (Criterion c : criterion) {
            crit.add(c);
        }
       
        return crit.list();
    }

    ....
}


I'm looking to add a similar method that returns results as a Map<ID,T> instead of a List<T>. At first I thought projections would be the answer, something like the following:

Code:
public Map<ID,T> findByCriteriaMap(Criterion... criterion) {

    Session session =
            ((HibernateEntityManager)getEntityManager()).getSession();
    Criteria crit = session.createCriteria(getEntityBeanType());

    for (Criterion c : criterion) {
        crit.add(c);
    }
       
    crit.setProjection( Projections.projectionList()
        .add( Projections.id() )
        .add( Projections.????? )
    );

   List<?> list = crit.list();

   Map<ID,T> map = new HashMap<ID,T>();

   for( Object result : list ) {
        Object[] arr = (Object[]) result;
        ID id = (ID) arr[0];
        T entity = (T) arr[1];
        map.put( id, entity );
   }

   return map;
}


However, this solution requires a projection that returns the entity itself, and not aggregate information or a specific property on that entity. I can't find a ResultTransformer to do this for me nor have I entered a Google query that has provided illumination.

Any suggestions?

Thanks,

Joel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 13, 2007 4:15 pm 
Newbie

Joined: Wed Dec 12, 2007 11:00 pm
Posts: 2
Well, I'd still like to know how to use Projections to return the entity itself, but until then, here's a solution using the session object:

Code:
protected Map<ID,T> findByCriteriaMap( Criterion... criterion) {
       
    Session session = ((HibernateEntityManager)getEntityManager()).getSession();

    Criteria crit = session.createCriteria(getEntityBeanType());
       
    for (Criterion c : criterion) {
        crit.add(c);
    }
       
    List<T> list = (List<T>) crit.list();

    Map<ID,T> map = new HashMap<ID,T>();

    for( T entity : list ) {
         ID id = (ID) (session.getIdentifier(entity) );
         map.put( id, entity );
    }

    return map;
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 3:21 pm 
Newbie

Joined: Fri May 11, 2007 3:53 pm
Posts: 12
Set a ResultTransformer on the critera like this:


criteria.setResultTransformer(Transformers.aliasToBean(YourEntity.class));

List<YourEntity> list = criteria.list();

You had to give your projections aliases for this to work. If you don't then you'll get back a null for property in your entity.


@SuppressWarnings("unchecked")
public void test() throws Exception {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
Criteria criteria = session.createCriteria(Queue.class);
ProjectionList projections = Projections.projectionList().add(Projections.property("queueId"), "queueId").add(
Projections.property("configurationXml"), "configurationXml");
criteria.setProjection(projections);

criteria.setResultTransformer(Transformers.aliasToBean(Queue.class));

List<Queue> list = criteria.list();

for (Queue queue : list) {
log.debug("whee:" + queue.getQueueId() + " " + queue.getConfigurationXml());
}

transaction.commit();
session.close();
}


And my Queue Entity class has six columns in the database and I did this test to get back only two. You can use this same code and take off the alias and see you'll get nothing back but nulls. It uses the alias to find the setter method that matches the pattern *setAlias*.


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