-->
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: Criteria API, associations
PostPosted: Wed Jan 20, 2010 11:14 am 
Newbie

Joined: Thu Mar 27, 2008 1:30 pm
Posts: 6
Hi,

I searched around and found that association mapping seems to be required when using the Critiria API. I'm asking because I have a project using hibernate 3.2 and I'm doing queries like this (HQL): (Entities mapped with annotations)

Code:
Session session = HibernateHelper.getCurrentSession();
Query query = session.createQuery("SELECT DISTINCT lg FROM OnpLogradouro lg, OnpMatricula mat, OnpMovimento mov " +
"WHERE mov.onpMovimentoPK.seqRoteiro = :rot AND mov.onpMovimentoPK.codReferencia = :ref " +
"AND lg.seqLogradouro = mat.seqLogradouro AND mov.onpMovimentoPK.seqMatricula = mat.seqMatricula")
.setString("ref", codReferencia).setLong("rot", seqRoteiro);
Those entities has no association mapping defined and the query still works. I thought the same was possible with criteria. So I tried: (Expression is just a wrapper for hibernate's Expression

Code:
ExpressionCollection condition = new ExpressionList();
condition.add(new Expression("mov.SeqMatricula", "mat.SeqMatricula", ExpressionOperation.Equal));
condition.add(new Expression("mov.SeqRoteiro", "1", ExpressionOperation.Equal));
condition.add(new Expression("mov.CodReferencia", "2009.12", ExpressionOperation.Equal));
   
Engine engine = ApplicationControl.getActive().getConfiguration().getPersistEngine();
EntityObjectCollection objetos = (EntityObjectList) engine.list(AcqMatricula.class, condition, new String[] { "AcqMovimento",  "mov", "AcqMatricula", "mat");

Code:
    public EntityObjectCollection list(Class entityClass, ExpressionCollection condition, OrderCollection order, Integer offSet, Integer maxSize, String[] tables) {
        Session session = openSession();
        EntityObjectCollection result = new EntityObjectList();
        Transaction transaction = startTrans(session);
       
        try {
            Criteria criteria = session.createCriteria(entityClass);

            for (int i=0; i < tables.length; i+=2) {
                criteria.createAlias(tables[i], tables[1+1]);
            }
           
            if(condition !=null){
                for(Expression expression : condition){
                    criteria.add(CriterionGenerator.make(expression));
                }
            }
           
            if(offSet > 0){
                criteria.setFirstResult(offSet);
            }
           
            if(maxSize > 0){
                criteria.setMaxResults(maxSize);
            }
           
            if(order !=null){
                for(br.com.strategos.api.persist.engines.Order orderItem : order){
                    criteria.addOrder(OrderGenerator.make(orderItem));
                }
            }
           
            List criteriaList = criteria.list();
           
            for(Object obj: criteriaList){
                EntityObject entity = (EntityObject) obj;
                entity.setStatus(EntityStatus.Load);
                entity.copyObject();
            }
           
            result.addAll(criteriaList);
           
            commit(transaction);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        } finally {
            disconnect(session);
        }
       
        return result;


And what I get is:
Quote:
org.hibernate.QueryException: could not resolve property: AcqMovimento of: br.com.strategos.acquacorporate.faturamento.AcqMatricula


So I'm confused now, do I really need to map associations to use the criteria api or I'm doing something wrong when trying to it?

TIA,
Sergio


Top
 Profile  
 
 Post subject: Re: Criteria API, associations
PostPosted: Thu Jan 21, 2010 9:44 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Well, the objects in the domain must be mapped with associations in order for Hibernate to process the association during the Criteria query.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: Criteria API, associations
PostPosted: Thu Jan 21, 2010 12:12 pm 
Newbie

Joined: Thu Mar 27, 2008 1:30 pm
Posts: 6
Well, I just find it weird that HQL doesn't need it. Anyway, thank you for answer!


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.