-->
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.  [ 6 posts ] 
Author Message
 Post subject: Help Needed with Criteria by Example
PostPosted: Thu Sep 21, 2006 4:18 am 
Newbie

Joined: Thu Aug 31, 2006 3:59 am
Posts: 3
I have a Pojo called Product as follows:

Public Class Product{
private ProductPK pk;
private String stockNum;
private Double price;

public Product(){
}

//getters and setters below
}

Note that the Product Pojo has a attribute of type ProductPK.
ProductPK represents a composite key. It looks as follows:

Public class ProductPK{
private String productCode;
private Date operativeDate;

public ProductPK(){
}

//getters and setters below
}


I wish to use the Criteria API and do a query by example.
If I search for all products matching a given stock number, i know the following works:

Product prod = new Product();
prod.setStockNum("45678");
Criteria c = session.createCriteria(Product .class);
c.add(Example.create(prod ));
List lst = c.list();
//This gives me back the products matching a stock number.

My problem is that I wish to do the query by ProductCode.
Now, ProductCode is a attribute of ProductPK. I thought the following
would work....but it didnt!

Product prod = new Product();
ProductPK pk = new ProductPK();
pk.setProductCode("A4567F");
prod.setPk(pk);
Criteria c = session.createCriteria(Product .class);
c.add(Example.create(prod ));
List lst = c.list();

This should have return me 1 object back since there is only 1 row in the table with that productCode. Instead it returns over 15000 rows.


Remember, that ProductPK is composed of a productCode attribute and a operativeDate attribute. In the above, I only set the productCode and left the operativeDate to null. If I set the operativeDate attribute too, then it works fine.

But I dont want to set the operativeDate since it is not known at runtime.
I am assuming that if your Criteria lies in your primary key class, then all attributes of that class must be populated in order for hibernate to identify a row in the table. Is this correct? IF that is the case, then the query by Example will not work!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 21, 2006 8:28 am 
Senior
Senior

Joined: Wed Aug 17, 2005 12:56 pm
Posts: 136
Location: Erie, PA (USA)
Can we see the SQL generated when the date is not included versus is included?

_________________
---- Don't forget to rate! ----


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 21, 2006 9:07 am 
Newbie

Joined: Thu Aug 31, 2006 3:59 am
Posts: 3
Firstly, in my original message, i forgot to add the following line:

//just before calling .list() method.
c.add(Restrictions.idEq(pk));


ok onto the generated query:

Without seting the operativeDate, the query generated is:

Hibernate: select this_.product_code as product1_0_, this_.operative_date as operative2_0_, this_.stock_no as stock3_2_0_, this_.pip_code as pip4_2_0_, this_.unit_cost as unit5_2_0_, this_.price_1 as price6_2_0_, this_.price_2 as price7_2_0_, this_.price_3 as price8_2_0_, this_.discontinued as disconti9_2_0_ from op_products_extra this_ where (1=1) and (this_.product_code = ? and this_.operative_date = ?)


When we set the operativeDate, the query generated is:

Hibernate: select this_.product_code as product1_0_, this_.operative_date as operative2_0_, this_.stock_no as stock3_2_0_, this_.pip_code as pip4_2_0_, this_.unit_cost as unit5_2_0_, this_.price_1 as price6_2_0_, this_.price_2 as price7_2_0_, this_.price_3 as price8_2_0_, this_.discontinued as disconti9_2_0_ from op_products_extra this_ where (1=1) and (this_.product_code = ? and this_.operative_date = ?)


The querys look the same. Im not sure what the 1=1 is all about though.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 24, 2006 8:07 pm 
Newbie

Joined: Mon Aug 29, 2005 1:14 am
Posts: 10
gurps_cov wrote:
The queries look the same. Im not sure what the 1=1 is all about though.


The 1=1 is just an artifact of automatically generating the SQL code. It's an always true where sub-clause, so it changes the query in absolutely no way.

It just makes it easier to generate code piecemeal (otherwise the code generator would have to re-parse the entire generated SQL string every time a where sub-clause was added).

(In particular, if a dummy 1=1 predicate is always added, every additional sub-clause can be in the form "and( WHATEVER )", without having to know if the existing where clause already contains a predicate. Without the dummy "1=1" predicate, the code generator would have to scan the existing clause to see if it needs to prefix the additional sub-clause with an "and".)

Ignore it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 6:39 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
http://www.hibernate.org/hib_docs/v3/re ... a-examples


"Version properties, identifiers and associations are ignored."


"c.add(Restrictions.idEq(pk));" is the part that is currently working. If you remove this, even after setting "operativeDate" it should not work.


Try with
c.add(Restrictions.eq("pk.productCode", "45678"))

and remove
c.add(Restrictions.idEq(pk));


Top
 Profile  
 
 Post subject: Re: Help Needed with Criteria by Example
PostPosted: Mon Sep 25, 2006 8:14 am 
Newbie

Joined: Mon Sep 25, 2006 7:42 am
Posts: 7
Public Class Product{
private ProductPK pk;
private String stockNum;
private Double price;

public Product(){
}

//getters and setters below
}

Note that the Product Pojo has a attribute of type ProductPK.
ProductPK represents a composite key. It looks as follows:

Public class ProductPK{
private String productCode;
private Date operativeDate;

public ProductPK(){
}

//getters and setters below
}

In Above code you should never have setters for PK class (ProductPK ) instead you should have a constructor like
public ProductPK(String productCode, Date operativeDate)
{
//Assign values
}
//No Setters
//you can have Getters.,
Then you would not get such a problem.,
This is recommend in EJB Entity Beans., Follow this you wont get such problems.
Thnx
Subbu

_________________
Never Give Up


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