-->
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.  [ 13 posts ] 
Author Message
 Post subject: Criteria with id
PostPosted: Tue May 04, 2004 6:03 am 
Beginner
Beginner

Joined: Fri Oct 17, 2003 4:11 am
Posts: 40
Another problem with Criteria (hibernate 2.1.3) :(

I'd like to get an object with a specific identifier (field named id of type Long with native generator):

Code:
Criteria criteria=session.createCriteria(HUtente.class);
HUtente hu=new HUtente();
hu.id=new Long(1);
Example example=Example.create(hu);
example.excludeZeroes();
criteria.add(example);
criteria.list();


But this return all the object of HUtente class! It seems the hu.id will be ignored... (if i put hu.name="A%" it works!)
Any suggest?

Gio


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 6:25 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
the id property is ignored in example queries. Just add a normal criterion.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 7:27 am 
Beginner
Beginner

Joined: Fri Oct 17, 2003 4:11 am
Posts: 40
Thanx!

Just another question about this:
so it's better to check if the id is set and use find instead of Criteria in this case?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 05, 2004 11:59 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
load will be better

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Bug?
PostPosted: Tue Jul 20, 2004 10:37 am 
Beginner
Beginner

Joined: Fri Mar 26, 2004 8:19 am
Posts: 49
michael wrote:
the id property is ignored in example queries. Just add a normal criterion.


I strongly feel this is a bug. I fervently love the QBE functionality, but this was completely unexpected when I encountered it yesterday.

Furthermore, are association classes also ignored in QBE? That is, if a child has an association to its parent, I have noticed that if you set only the parent, and do a QBE, hibernate ignores it as well.

This works:

Code:
    public void testSearchOrders() throws Exception {
        Session session = sf.openSession();

        // Account resultAcct = (Account) session.get(Account.class, new Long(1001084));

        //Order order = new Order();
        //order.setAccount(resultAcct);

        List orderResults = session.createCriteria(Order.class).add(Expression.eq("account.accountId", new Long(1001084))).list();
        assertTrue(orderResults.size() > 0);
       
        for (Iterator iter = orderResults.iterator(); iter.hasNext();) {
            Order order2 = (Order) iter.next();
            assertTrue(order2.getAccount().getAccountId().equals(resultAcct.getAccountId()));
        }
     session.close();
    }


This does not work:

Code:
public void testSearchOrders() throws Exception {
        Session session = sf.openSession();

        Account account = new Account();
// DOES NOT WORK      account.setAccountId(new Long(1001084));
        account.setAccountName("some valid name");

        List acctRes = session.createCriteria(Account.class).add(Example.create(account)).list();
        Account resultAcct = (Account)acctRes.get(0);
       
        Order order = new Order();
        order.setAccount(resultAcct);

        List orderResults = session.createCriteria(Order.class).add(Example.create(order)).list();

        for (Iterator iter = orderResults.iterator(); iter.hasNext();) {
           Order order2 = (Order) iter.next();
           // assertion fails
     assertTrue(order2.getAccount().getAccountId().equals(resultAcct.getAccountId()));
        }
     session.close();
    }


Account mapping snippet ()
Code:
    <!-- bi-directional one-to-many association to Order -->
    <set
        name="orders"
        lazy="false"
        inverse="true"
        cascade="all"
    >
        <key>
            <column name="ACCOUNT_ID" />
        </key>
        <one-to-many
            class="com.cybera.seneca.persistence.Order"
        />
    </set>

Order mapping snippet (perhaps the problem is in here?)
Code:
    <!-- associations -->
    <!-- bi-directional many-to-one association to Account -->
    <many-to-one
        name="account"
        class="com.cybera.seneca.persistence.Account"
        not-null="true"
    >
        <column name="ACCOUNT_ID" />
    </many-to-one>


thanks in advance. I'm using hibernate 2.1.4.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 10:43 am 
Beginner
Beginner

Joined: Fri Mar 26, 2004 8:19 am
Posts: 49
I updated and built Hibernate2 from CVS just now and changed my child mapping as follows:

Code:
    <!-- associations -->
    <!-- bi-directional many-to-one association to Account -->
    <many-to-one
        name="account"
        class="com.cybera.seneca.persistence.Account"
        not-null="true"
        cascade="all"
    >
        <column name="ACCOUNT_ID" />
    </many-to-one>


However, the assertions mentioned above still fail.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 10:50 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Code:
   private boolean isPropertyIncluded(Object value, String name, Type type) {
      return !excludedProperties.contains(name) &&
         !type.isAssociationType() &&
         selector.include(value, name, type);
   }


If you want to constrain an association, you must use createCriteria(), and a separate Criterion.


Note that I am very happy with the current, intended, functionality.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 11:54 am 
Beginner
Beginner

Joined: Fri Mar 26, 2004 8:19 am
Posts: 49
gavin wrote:
If you want to constrain an association, you must use createCriteria(), and a separate Criterion.

Note that I am very happy with the current, intended, functionality.


Thanks very much, Gavin, I have a patch to Example.java which allows associations to be included. Is this futile?

Secondly, I do not understand your instructions above. I'll see what I can do.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 12:00 pm 
Beginner
Beginner

Joined: Fri Mar 26, 2004 8:19 am
Posts: 49
My design that motivates this change is as follows, I'd like to pass an example object off to a Hibernate 'adapter' I've created. I'd like the adapter to know nothing about the object itself, just to load it up and return. The best way I think to do this is to have associations also be a selector to the criteria...sometimes a parent association is all I'll have at that point.

I'm thoroughly impressed you responded directly and so quickly!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 1:24 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You can easily use the Hibernate ClassMetadata object to build something on top of the Example API. Or, you could simply write your own Criterion implementation (Criteria API is extensible).


The code I quoted was from the Example class, to show you that this functionality was intended and explicitly coded.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 2:14 pm 
Beginner
Beginner

Joined: Fri Mar 26, 2004 8:19 am
Posts: 49
Dude, you rule!

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 12:57 pm 
Beginner
Beginner

Joined: Tue May 31, 2005 1:16 pm
Posts: 35
Location: Bogota, Colombia
I am having problems with this, and still dont understand why it is not a bug.

I have a DAO implementation that has a method intended to return the instances that match the properties of an Example object (including associations).

The method looks like this:
Code:
public List obtenerBeans(Bean bean) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
        final Bean beanFinal = (Bean)BeanUtils.cloneBean(bean);
        return getHibernateTemplate().executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                Criteria criteria = session.createCriteria(Bean.class);
                criteria.add(Example.create((beanFinal)));
                List result = criteria.list();
                return result;
            }
        });
    }

However, if there's an association from Class A to class B, and I set the example as

A a =a.setB(b)

before calling the method in my dao, the method returns all the rows, ingnoring the association. This is a consequence of what has been said above in this post.

My question is, how can I cleanly fix this in my obtenerBeans method?


Please help!

_________________
Julian Garcia


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 3:04 pm 
Beginner
Beginner

Joined: Tue May 31, 2005 1:16 pm
Posts: 35
Location: Bogota, Colombia
How should I modify my dao to make the Example class work with relationships as well. Do I have to modify Example class?

Please HELP!

_________________
Julian Garcia


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