-->
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.  [ 4 posts ] 
Author Message
 Post subject: query by example fails using reference object
PostPosted: Wed Feb 23, 2005 4:11 pm 
Newbie

Joined: Wed Feb 23, 2005 2:08 pm
Posts: 17
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

I am expecting back only purchased products where the product matches. Instead I get back all purchased product objects, regardless of product, from the database. For queries where I set properties such as a String , int etc this works fine. It fails when I pass in a refernce object. What am I doing wrong? Any help would be greatly appreciated and hopefully I submitted this ok :)

Scott


Hibernate version:2.1

Mapping documents:<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.gen.ipdsserver.persistence.PurchasedProduct" table="PURCHASED_PRODUCT">
<meta attribute="class-description">

Represents a purchased product.
</meta>

<id name="id" type="long" column="PURCHASED_PRODUCT_ID">
<meta attribute="scope-set">protected</meta>
<meta attribute="field-description">Unique identity - automatically generated</meta>
<generator class="native" />
</id>

<many-to-one name="contact" column="CONTACT_ID"
class="com.gen.ipdsserver.persistence.Contact"
cascade="save-update"
not-null="true"
outer-join="true"/>

<many-to-one name="lineItem" column="LINE_ITEM_ID"
class="com.gen.ipdsserver.persistence.LineItem"
cascade="none"
not-null="false"
outer-join="true"/>

<many-to-one name="product" column="PRODUCT_OFFERING_ID"
class="com.gen.ipdsserver.persistence.ProductOffering"
cascade="none"
not-null="true"
outer-join="true"/>


<property name="productIdNumber" type="string">
<meta attribute="use-in-tostring">true</meta>
<meta attribute="field-description">The serial number of the purchased product</meta>
<column name="PRODUCT_ID_NUMBER" length="30" />
</property>

<property name="loginName" type="string">
<meta attribute="use-in-tostring">true</meta>
<meta attribute="field-description">The login name for the purchased product</meta>
<column name="LOGIN_NAME" length="25" />
</property>

<property name="password" type="string">
<meta attribute="use-in-tostring">true</meta>
<meta attribute="field-description">The password for the purchased product</meta>
<column name="PASSWORD" length="30" />
</property>

<property name="active" type="boolean">
<meta attribute="use-in-tostring">true</meta>
<meta attribute="field-description">Is the purchased product active</meta>
<column name="ACTIVE" not-null="true" />
</property>

<property name="expirationDate" type="timestamp">
<meta attribute="field-description">The purchased product expiration date</meta>
<column name="EXPIRATION_DATE" not-null="true" />
</property>

<property name="lastSaved" type="timestamp">
<meta attribute="field-description">The purchased product save date</meta>
<column name="LAST_SAVED" not-null="true" />
</property>

</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():session = openSession();

HibernateService hibernateService = HibernateService.getInstance(configurator.getHibernateConfigurationProperty());
ProductOffering product=detail.getProduct();
product=(ProductOffering)hibernateService.getUniqueResult(product);
if(product==null)
throw new ServiceException("product offering not found in database");
PurchasedProduct pp= new PurchasedProduct();
pp.setActive(true);
pp.setProduct(product);
// this gets a list of purchased products
List purchasedProducts=hibernateService.getResults(pp);

// this is my getResults method that i call

Example example = Example.create(exampleCriteria);
example.excludeZeroes();
example.ignoreCase();
if (!exactMatch) {
example.enableLike();
}

Criteria criteria = session.createCriteria(exampleCriteria.getClass());
criteria.add(example);
if (order != null) {
criteria.addOrder(order);
}

results = criteria.list();
}
catch (HibernateException he) {
he.printStackTrace();
throw new PersistenceException(he);
}
finally {
endSession(session);
}


Full stack trace of any exception that occurs:

Name and version of the database you are using:PostgreSQL

The generated SQL (show_sql=true):14:32:48,955 INFO [STDOUT] Hibernate: select productoff0_.PRODUCT_OFFERING_ID a
s PRODUCT_1_3_, productoff0_.UPGRADE_ID as UPGRADE_ID3_, productoff0_.PRODUCT_OF
FERING_TYPE_ID as PRODUCT_3_3_, productoff0_.NAME as NAME3_, productoff0_.DESCRI
PTION as DESCRIPT5_3_, productoff0_.RECURRING as RECURRING3_, productoff0_.PAYME
NT_PERIOD as PAYMENT_7_3_, productoff0_.INSTALLMENTS as INSTALLM8_3_, productoff
0_.PRICE as PRICE3_, productoff0_.DATE_CREATED as DATE_CR10_3_, productoff0_.LAS
T_SAVED as LAST_SAVED3_, productoff1_.PRODUCT_OFFERING_ID as PRODUCT_1_0_, produ
ctoff1_.UPGRADE_ID as UPGRADE_ID0_, productoff1_.PRODUCT_OFFERING_TYPE_ID as PRO
DUCT_3_0_, productoff1_.NAME as NAME0_, productoff1_.DESCRIPTION as DESCRIPT5_0_
, productoff1_.RECURRING as RECURRING0_, productoff1_.PAYMENT_PERIOD as PAYMENT_
7_0_, productoff1_.INSTALLMENTS as INSTALLM8_0_, productoff1_.PRICE as PRICE0_,
productoff1_.DATE_CREATED as DATE_CR10_0_, productoff1_.LAST_SAVED as LAST_SAVED
0_, productoff2_.PRODUCT_OFFERING_TYPE_ID as PRODUCT_1_1_, productoff2_.PRODUCT_
OFFERING_TYPE as PRODUCT_2_1_, productoff2_.LAST_SAVED as LAST_SAVED1_, producto
ff3_.PRODUCT_OFFERING_TYPE_ID as PRODUCT_1_2_, productoff3_.PRODUCT_OFFERING_TYP
E as PRODUCT_2_2_, productoff3_.LAST_SAVED as LAST_SAVED2_ from PRODUCT_OFFERING
productoff0_ left outer join PRODUCT_OFFERING productoff1_ on productoff0_.UPGR
ADE_ID=productoff1_.PRODUCT_OFFERING_ID left outer join PRODUCT_OFFERING_TYPE pr
oductoff2_ on productoff1_.PRODUCT_OFFERING_TYPE_ID=productoff2_.PRODUCT_OFFERIN
G_TYPE_ID left outer join PRODUCT_OFFERING_TYPE productoff3_ on productoff0_.PRO
DUCT_OFFERING_TYPE_ID=productoff3_.PRODUCT_OFFERING_TYPE_ID where productoff0_.P
RODUCT_OFFERING_ID=?


Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 23, 2005 5:35 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Chapter 12.6 says:
Version properties, identifiers and associations are ignored.

The last example of the chapter tells you how to deal with associations in example queries.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 23, 2005 6:25 pm 
Newbie

Joined: Wed Feb 23, 2005 2:08 pm
Posts: 17
Ernst, thanks for the info. I am still having troubles though

Scott

17:01:00,841 INFO [STDOUT] net.sf.hibernate.QueryException: could not resolve p
roperty: com.gen.ipdsserver.persistence.ProductOffering of: com.iatrogen.ip
dsserver.persistence.PurchasedProduct

HibernateService hibernateService = HibernateService.getInstance(configurator.getHibernateConfigurationProperty());
ProductOffering product=detail.getProduct();
product=(ProductOffering)hibernateService.getUniqueResult(product);
if(product==null)
throw new ServiceException("product offering not found in database");

PurchasedProduct pp= new PurchasedProduct();
pp.setActive(true);
pp.setProduct(product);
// this gets a list of purchased products
List purchasedProducts=hibernateService.getResultsWithAssociation(pp,product,ProductOffering.class.getName());


public List getResultsWithAssociation(Object exampleCriteria, Object assocObj,String assocName)
throws PersistenceException {
List results = null;
Session session = null;

try {
session = openSession();

Example example = Example.create(exampleCriteria);
example.excludeZeroes();
example.ignoreCase();


Criteria criteria = session.createCriteria(exampleCriteria.getClass());
criteria.add(example);
criteria.createCriteria(assocName);
criteria.add(Example.create(assocObj));
results = criteria.list();


Top
 Profile  
 
 Post subject: hibernate.cglib.use_reflection_optimizer
PostPosted: Thu Feb 24, 2005 8:15 am 
Newbie

Joined: Wed Feb 23, 2005 2:08 pm
Posts: 17
well I believe I passed in the wrong name for the association. from the mapping it should be "product". Now I get the following exception.

06:43:36,982 INFO [STDOUT] net.sf.hibernate.PropertyAccessException: exception
setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=
false for more info) getter of com.gen.ipdsserver.persistence.PurchasedProd
uct.?
at net.sf.hibernate.persister.AbstractEntityPersister.getPropertyValues(
AbstractEntityPersister.java:234)
at net.sf.hibernate.expression.Example.toSqlString(Example.java:165)
at net.sf.hibernate.loader.CriteriaLoader.<init>(CriteriaLoader.java:66)
....
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:683)
at java.lang.Thread.run(Thread.java:534)
Caused by: java.lang.ClassCastException
at com.iatrogen.ipdsserver.persistence.PurchasedProduct$$BulkBeanByCGLIB
$$f8c0c4f2.getPropertyValues(<generated>)
at net.sf.cglib.beans.BulkBean.getPropertyValues(BulkBean.java:86)
at net.sf.hibernate.persister.AbstractEntityPersister.getPropertyValues(
AbstractEntityPersister.java:230)
... 85 more
I added the following to the hibernate cfg.xml as recommended but see no change and no additional logging. Any suggestions on what I am doing wrong? thanks in advance.

Scott

<property name="hibernate.cglib.use_reflection_optimizer">false</property>


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