-->
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 and Collections
PostPosted: Mon Feb 05, 2007 9:26 pm 
Newbie

Joined: Mon Feb 05, 2007 9:16 pm
Posts: 4
Ok, I've spent a few hours searching forums, documentation, FAQ... and I am no closer to solving my problem.

Simple scenario:

@Entity
class Customer {
@OneToMany
List<Pet> pets;
}

@Entity
class Pet {
Breed breed;
String name;
}


I'd like to use the Criteria API to search for Customers with pets that either match a given breed or a given name.

For the life of me, I cannot solve the problem.

I've tried, as per the Hibernate documentation:
Criteria crit = session.createCriteria(Customer.class).createCriteria("pets").add("name", templatePet.getName());

I'd much rather use an Example Pet:
Criteria crit = session.createCriteria(Customer.class).createCriteria("pets").add(Example.createExample(templatePet));

And I get no results, even though I know my test is set up correctly to return the one result. I can return Customer objects by queries on non-Entity and Embedded fields, just not this Collection.

All I really need is one example of how to make this happen, let's not get started on having a collection of non-Entities, just let me query for Entities in a collection first. :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 2:43 pm 
Newbie

Joined: Mon Feb 05, 2007 9:16 pm
Posts: 4
Ok, so I've made my situation work, but it leads me to another question about Hibernate and flushing sessions (I think).

For clarity's sake, I created two classes. TestModelClass has a List of TestModelClass2. TestModelClass2 has two String fields. I am using Criteria queries and QBE to search for TestModelClass giving only an example of TestModelClass2 with one String field filled in.

I was running a test case and persisting the test objects and searching within the same transaction. This was the SQL that hibernate listed:

10:32:30,640 DEBUG SQL:393 - insert into TestModelClass2 (version, string1, string2) values (?, ?, ?)
10:32:30,671 DEBUG SQL:393 - insert into TestModelClass2 (version, string1, string2) values (?, ?, ?)
10:32:30,687 DEBUG SQL:393 - insert into TestModelClass (version) values (?)
10:32:30,843 DEBUG SQL:393 - select this_.id as id25_1_, this_.version as version25_1_, member3_.TestModelClass_id as TestMode1_, testmodelc1_.id as member2_, testmodelc1_.id as id26_0_, testmodelc1_.version as version26_0_, testmodelc1_.string1 as string3_26_0_, testmodelc1_.string2 as string4_26_0_ from TestModelClass this_ inner join TestModelClass_TestModelClass2 member3_ on this_.id=member3_.TestModelClass_id inner join TestModelClass2 testmodelc1_ on member3_.member_id=testmodelc1_.id where (1=1) and (testmodelc1_.string1=?)

Conspicuously missing are the lines updating the List<TestModelClass2> relation.

I ran the tests again, this time committing the transaction where I persisted my test objects and starting a new transaction for the search. Here is that output:

10:32:30,640 DEBUG SQL:393 - insert into TestModelClass2 (version, string1, string2) values (?, ?, ?)
10:32:30,671 DEBUG SQL:393 - insert into TestModelClass2 (version, string1, string2) values (?, ?, ?)
10:32:30,687 DEBUG SQL:393 - insert into TestModelClass (version) values (?)
10:32:30,718 DEBUG SQL:393 - insert into TestModelClass_TestModelClass2 (TestModelClass_id, member_id) values (?, ?)
10:32:30,718 DEBUG SQL:393 - insert into TestModelClass_TestModelClass2 (TestModelClass_id, member_id) values (?, ?)
10:32:30,843 DEBUG SQL:393 - select this_.id as id25_1_, this_.version as version25_1_, member3_.TestModelClass_id as TestMode1_, testmodelc1_.id as member2_, testmodelc1_.id as id26_0_, testmodelc1_.version as version26_0_, testmodelc1_.string1 as string3_26_0_, testmodelc1_.string2 as string4_26_0_ from TestModelClass this_ inner join TestModelClass_TestModelClass2 member3_ on this_.id=member3_.TestModelClass_id inner join TestModelClass2 testmodelc1_ on member3_.member_id=testmodelc1_.id where (1=1) and (testmodelc1_.string1=?)

Why did I not get thsoe two lines showing my list was saved the first time? This is fairly annoying because its nice to run test cases without changing the database.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 3:34 pm 
Newbie

Joined: Mon Feb 05, 2007 9:16 pm
Posts: 4
Ok, well, I can call flush() before I do my search so I don't have to commit the transaction to the DB.

But, I still don't understand why hibernate won't update the Collections when I persist the classes.

Also, I had to use DISTINCT_ROOT_ENTITY to prevent the multiple copies of the same object, which is annoying (and apparently prevents me from doing pagination?)


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.