You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Schmidt <cl...@gmail.com> on 2008/02/13 20:32:05 UTC

How to check for null values when using proxies?

Hi,

Consider that an object A has an association with an object B. Also that
dynamic proxies are in use so that when object A is loaded, a proxy to B is
created and the object B itself is not materialized. Sometimes, before
taking an action, I need to verify if there is a B object associated to A.
This check is done as follows:
if (a.getB() == null) {...}

The problem is that even if there isn't a B associated with A, OJB returns
false because there is always a proxy between A and B. The check is actually
done using the proxy reference which is always present.
How can we handle that?

Thank in advance!
-- 
View this message in context: http://www.nabble.com/How-to-check-for-null-values-when-using-proxies--tp15465050p15465050.html
Sent from the Apache DB - ObjectRelationalBridge Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: How to check for null values when using proxies?

Posted by Armin Waibel <ar...@apache.org>.

Armin Waibel wrote:
> 
>> In the 1:n associations I set auto-retrieve="false"
>> and proxy="dynamic". As my classes do not implement interfaces, I'm using
>> CGLib as ProxyFactoryClass.
> 

Now I get your point. You are using dynamic proxies for the referenced 
objects (and maybe a proxy reference for the 1:n relation too) and OJB 
return the wrong instance for all subclasses.


>> Another problem that came up after I started using proxies is in the
>> hierarchy mapping. I'm using just one table to map my class hierarchy. 
>> So,
>> this table has a CLASSNAME field to identify which class to load. In the
>> constructor of my base class I have a
>> setOjbConcreteClass(this.getClass().getName()) call. Before using 
>> proxies,
>> the class name was configured correctly. But after, instead of having the
>> real object class name, the application configures the classname field 
>> using
>> the proxy class name.
>>
>> I assume there should be something wrong with my mapping files or some 
>> other
>> point in my application. As said earlier, I believe proxies should be
>> transparent. Did someone here experience the same problems?
> 
> Hm, I will have a look at this tomorrow (will try to reproduce your issue).

I can reproduce the issue and create a bug report:
http://issues.apache.org/jira/browse/OJB-138

Setup some new tests and it seems that I can fix it soon.


regards,
Armin

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: How to check for null values when using proxies?

Posted by Armin Waibel <ar...@apache.org>.
Hi,

Schmidt wrote:
> Hi Armin,
> 
> Thank you for the answer.
> 
> I'm using the 1.0.4 version. And the same problem happens with 1:n
> associations as well.

If you use lazy loading with 1:n or m:n relation OJB always create a 
collection proxy object (this helps to avoid a DB query on object 
materialization). Recommended way to check n-side existence is to use 
Collection.size() method. In this case OJB performs a count-query to 
verify the number of the n-side objects.


> In the 1:n associations I set auto-retrieve="false"
> and proxy="dynamic". As my classes do not implement interfaces, I'm using
> CGLib as ProxyFactoryClass.

The setting proxy="dynamic" is an illegal attribute value for reference- 
and collection-descriptors (only true/false is valid). It doesn't make 
sense (please correct me if am wrong) to use auto-retrieve="false" 
(prevent materialization of the 1:n or m:n relation)
http://db.apache.org/ojb/docu/guides/basic-technique.html#Setting+Load%2C+Update%2C+and+Delete+Cascading
in combination with proxy="true" (lazy-loading).

> I debugged the source code and verified that the collection representing the
> n side of the 1:n association is filled with proxy references. That's good
> because the real object are not materialized, but when I check for nullity,
> the result is false due to proxy references. The solution (ugly) to bypass
> this problem is in the ProxyHelper.getRealObject method. But this solution
> is invasive, proxies should be transparent.

Please use Collection.size()

> 
> Another problem that came up after I started using proxies is in the
> hierarchy mapping. I'm using just one table to map my class hierarchy. So,
> this table has a CLASSNAME field to identify which class to load. In the
> constructor of my base class I have a
> setOjbConcreteClass(this.getClass().getName()) call. Before using proxies,
> the class name was configured correctly. But after, instead of having the
> real object class name, the application configures the classname field using
> the proxy class name.
> 
> I assume there should be something wrong with my mapping files or some other
> point in my application. As said earlier, I believe proxies should be
> transparent. Did someone here experience the same problems?

Hm, I will have a look at this tomorrow (will try to reproduce your issue).

regards,
Armin

> 
> Thank you again!
> 
> 
> Armin Waibel wrote:
>> Hi,
>>
>> Schmidt wrote:
>>> Hi,
>>>
>>> Consider that an object A has an association with an object B. Also that
>>> dynamic proxies are in use so that when object A is loaded, a proxy to B
>>> is
>>> created and the object B itself is not materialized. Sometimes, before
>>> taking an action, I need to verify if there is a B object associated to
>>> A.
>>> This check is done as follows:
>>> if (a.getB() == null) {...}
>>>
>>> The problem is that even if there isn't a B associated with A, OJB
>>> returns
>>> false because there is always a proxy between A and B. The check is
>>> actually
>>> done using the proxy reference which is always present.
>>> How can we handle that?
>>>
>> Which version of OJB do you use? I try to reproduce this with the 
>> current source (OJB_1_0_RELEASE branch) without success. If class 
>> Article has a 1:1 relation to ProductGroup and I search for an Article 
>> without a ProductGroup I get:
>>
>> Criteria c = new Criteria().addEqualTo("articleName", name + "without
>> PG");
>> q = QueryFactory.newQuery(ArticleWithReferenceProxy.class, c);
>> Article br = (ArticleWithReferenceProxy) broker.getObjectByQuery(q);
>> assertNotNull(br);
>> assertNull(br.getProductGroup());
>>
>> Could it be that the association between A and B is a 1:n relation?
>>
>> regards,
>> Armin
>>
>>> Thank in advance!
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
>> For additional commands, e-mail: ojb-user-help@db.apache.org
>>
>>
>>
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: How to check for null values when using proxies?

Posted by Armin Waibel <ar...@apache.org>.
Hi,

Schmidt wrote:
> Hi again Armin,
> 
> I saw some discussions regarding the AbstractIndirectionHandler class, maybe
> the method "public Object invoke" is outdated in the OJB version in use by
> me. I'm using OJB1.0.4 and the jar file date is 31/12/2005. Is that correct?

yep!

> Is there any other version available? If so, please point me out where can I
> get it.
> Thank you!

You can test the upcoming 1.0.5 version. The 1.0.5rc1 is stable but 
contain some specific query bugs. Details see this thread:

http://www.mail-archive.com/ojb-user%40db.apache.org/msg16078.html

regards,
Armin

> 
> 
> Schmidt wrote:
>> Hi Armin,
>>
>> Thank you for the answer.
>>
>> I'm using the 1.0.4 version. And the same problem happens with 1:n
>> associations as well. In the 1:n associations I set auto-retrieve="false"
>> and proxy="dynamic". As my classes do not implement interfaces, I'm using
>> CGLib as ProxyFactoryClass.
>> I debugged the source code and verified that the collection representing
>> the n side of the 1:n association is filled with proxy references. That's
>> good because the real object are not materialized, but when I check for
>> nullity, the result is false due to proxy references. The solution (ugly)
>> to bypass this problem is in the ProxyHelper.getRealObject method. But
>> this solution is invasive, proxies should be transparent.
>>
>> Another problem that came up after I started using proxies is in the
>> hierarchy mapping. I'm using just one table to map my class hierarchy. So,
>> this table has a CLASSNAME field to identify which class to load. In the
>> constructor of my base class I have a
>> setOjbConcreteClass(this.getClass().getName()) call. Before using proxies,
>> the class name was configured correctly. But after, instead of having the
>> real object class name, the application configures the classname field
>> using the proxy class name.
>>
>> I assume there should be something wrong with my mapping files or some
>> other point in my application. As said earlier, I believe proxies should
>> be transparent. Did someone here experience the same problems?
>>
>> Thank you again!
>>
>>
>> Armin Waibel wrote:
>>> Hi,
>>>
>>> Schmidt wrote:
>>>> Hi,
>>>>
>>>> Consider that an object A has an association with an object B. Also that
>>>> dynamic proxies are in use so that when object A is loaded, a proxy to B
>>>> is
>>>> created and the object B itself is not materialized. Sometimes, before
>>>> taking an action, I need to verify if there is a B object associated to
>>>> A.
>>>> This check is done as follows:
>>>> if (a.getB() == null) {...}
>>>>
>>>> The problem is that even if there isn't a B associated with A, OJB
>>>> returns
>>>> false because there is always a proxy between A and B. The check is
>>>> actually
>>>> done using the proxy reference which is always present.
>>>> How can we handle that?
>>>>
>>> Which version of OJB do you use? I try to reproduce this with the 
>>> current source (OJB_1_0_RELEASE branch) without success. If class 
>>> Article has a 1:1 relation to ProductGroup and I search for an Article 
>>> without a ProductGroup I get:
>>>
>>> Criteria c = new Criteria().addEqualTo("articleName", name + "without
>>> PG");
>>> q = QueryFactory.newQuery(ArticleWithReferenceProxy.class, c);
>>> Article br = (ArticleWithReferenceProxy) broker.getObjectByQuery(q);
>>> assertNotNull(br);
>>> assertNull(br.getProductGroup());
>>>
>>> Could it be that the association between A and B is a 1:n relation?
>>>
>>> regards,
>>> Armin
>>>
>>>> Thank in advance!
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
>>> For additional commands, e-mail: ojb-user-help@db.apache.org
>>>
>>>
>>>
>>
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: How to check for null values when using proxies?

Posted by Schmidt <cl...@gmail.com>.
Hi again Armin,

I saw some discussions regarding the AbstractIndirectionHandler class, maybe
the method "public Object invoke" is outdated in the OJB version in use by
me. I'm using OJB1.0.4 and the jar file date is 31/12/2005. Is that correct?
Is there any other version available? If so, please point me out where can I
get it.
Thank you!


Schmidt wrote:
> 
> Hi Armin,
> 
> Thank you for the answer.
> 
> I'm using the 1.0.4 version. And the same problem happens with 1:n
> associations as well. In the 1:n associations I set auto-retrieve="false"
> and proxy="dynamic". As my classes do not implement interfaces, I'm using
> CGLib as ProxyFactoryClass.
> I debugged the source code and verified that the collection representing
> the n side of the 1:n association is filled with proxy references. That's
> good because the real object are not materialized, but when I check for
> nullity, the result is false due to proxy references. The solution (ugly)
> to bypass this problem is in the ProxyHelper.getRealObject method. But
> this solution is invasive, proxies should be transparent.
> 
> Another problem that came up after I started using proxies is in the
> hierarchy mapping. I'm using just one table to map my class hierarchy. So,
> this table has a CLASSNAME field to identify which class to load. In the
> constructor of my base class I have a
> setOjbConcreteClass(this.getClass().getName()) call. Before using proxies,
> the class name was configured correctly. But after, instead of having the
> real object class name, the application configures the classname field
> using the proxy class name.
> 
> I assume there should be something wrong with my mapping files or some
> other point in my application. As said earlier, I believe proxies should
> be transparent. Did someone here experience the same problems?
> 
> Thank you again!
> 
> 
> Armin Waibel wrote:
>> 
>> Hi,
>> 
>> Schmidt wrote:
>>> Hi,
>>> 
>>> Consider that an object A has an association with an object B. Also that
>>> dynamic proxies are in use so that when object A is loaded, a proxy to B
>>> is
>>> created and the object B itself is not materialized. Sometimes, before
>>> taking an action, I need to verify if there is a B object associated to
>>> A.
>>> This check is done as follows:
>>> if (a.getB() == null) {...}
>>> 
>>> The problem is that even if there isn't a B associated with A, OJB
>>> returns
>>> false because there is always a proxy between A and B. The check is
>>> actually
>>> done using the proxy reference which is always present.
>>> How can we handle that?
>>> 
>> 
>> Which version of OJB do you use? I try to reproduce this with the 
>> current source (OJB_1_0_RELEASE branch) without success. If class 
>> Article has a 1:1 relation to ProductGroup and I search for an Article 
>> without a ProductGroup I get:
>> 
>> Criteria c = new Criteria().addEqualTo("articleName", name + "without
>> PG");
>> q = QueryFactory.newQuery(ArticleWithReferenceProxy.class, c);
>> Article br = (ArticleWithReferenceProxy) broker.getObjectByQuery(q);
>> assertNotNull(br);
>> assertNull(br.getProductGroup());
>> 
>> Could it be that the association between A and B is a 1:n relation?
>> 
>> regards,
>> Armin
>> 
>>> Thank in advance!
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
>> For additional commands, e-mail: ojb-user-help@db.apache.org
>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-check-for-null-values-when-using-proxies--tp15465050p15502984.html
Sent from the Apache DB - ObjectRelationalBridge Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: How to check for null values when using proxies?

Posted by Schmidt <cl...@gmail.com>.
Hi Armin,

Thank you for the answer.

I'm using the 1.0.4 version. And the same problem happens with 1:n
associations as well. In the 1:n associations I set auto-retrieve="false"
and proxy="dynamic". As my classes do not implement interfaces, I'm using
CGLib as ProxyFactoryClass.
I debugged the source code and verified that the collection representing the
n side of the 1:n association is filled with proxy references. That's good
because the real object are not materialized, but when I check for nullity,
the result is false due to proxy references. The solution (ugly) to bypass
this problem is in the ProxyHelper.getRealObject method. But this solution
is invasive, proxies should be transparent.

Another problem that came up after I started using proxies is in the
hierarchy mapping. I'm using just one table to map my class hierarchy. So,
this table has a CLASSNAME field to identify which class to load. In the
constructor of my base class I have a
setOjbConcreteClass(this.getClass().getName()) call. Before using proxies,
the class name was configured correctly. But after, instead of having the
real object class name, the application configures the classname field using
the proxy class name.

I assume there should be something wrong with my mapping files or some other
point in my application. As said earlier, I believe proxies should be
transparent. Did someone here experience the same problems?

Thank you again!


Armin Waibel wrote:
> 
> Hi,
> 
> Schmidt wrote:
>> Hi,
>> 
>> Consider that an object A has an association with an object B. Also that
>> dynamic proxies are in use so that when object A is loaded, a proxy to B
>> is
>> created and the object B itself is not materialized. Sometimes, before
>> taking an action, I need to verify if there is a B object associated to
>> A.
>> This check is done as follows:
>> if (a.getB() == null) {...}
>> 
>> The problem is that even if there isn't a B associated with A, OJB
>> returns
>> false because there is always a proxy between A and B. The check is
>> actually
>> done using the proxy reference which is always present.
>> How can we handle that?
>> 
> 
> Which version of OJB do you use? I try to reproduce this with the 
> current source (OJB_1_0_RELEASE branch) without success. If class 
> Article has a 1:1 relation to ProductGroup and I search for an Article 
> without a ProductGroup I get:
> 
> Criteria c = new Criteria().addEqualTo("articleName", name + "without
> PG");
> q = QueryFactory.newQuery(ArticleWithReferenceProxy.class, c);
> Article br = (ArticleWithReferenceProxy) broker.getObjectByQuery(q);
> assertNotNull(br);
> assertNull(br.getProductGroup());
> 
> Could it be that the association between A and B is a 1:n relation?
> 
> regards,
> Armin
> 
>> Thank in advance!
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-check-for-null-values-when-using-proxies--tp15465050p15499929.html
Sent from the Apache DB - ObjectRelationalBridge Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: How to check for null values when using proxies?

Posted by Armin Waibel <ar...@apache.org>.
Hi,

Schmidt wrote:
> Hi,
> 
> Consider that an object A has an association with an object B. Also that
> dynamic proxies are in use so that when object A is loaded, a proxy to B is
> created and the object B itself is not materialized. Sometimes, before
> taking an action, I need to verify if there is a B object associated to A.
> This check is done as follows:
> if (a.getB() == null) {...}
> 
> The problem is that even if there isn't a B associated with A, OJB returns
> false because there is always a proxy between A and B. The check is actually
> done using the proxy reference which is always present.
> How can we handle that?
> 

Which version of OJB do you use? I try to reproduce this with the 
current source (OJB_1_0_RELEASE branch) without success. If class 
Article has a 1:1 relation to ProductGroup and I search for an Article 
without a ProductGroup I get:

Criteria c = new Criteria().addEqualTo("articleName", name + "without PG");
q = QueryFactory.newQuery(ArticleWithReferenceProxy.class, c);
Article br = (ArticleWithReferenceProxy) broker.getObjectByQuery(q);
assertNotNull(br);
assertNull(br.getProductGroup());

Could it be that the association between A and B is a 1:n relation?

regards,
Armin

> Thank in advance!

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: How to check for null values when using proxies?

Posted by Schmidt <cl...@gmail.com>.
Hasn't anybody here ever faced this problem before?
-- 
View this message in context: http://www.nabble.com/How-to-check-for-null-values-when-using-proxies--tp15465050p15490516.html
Sent from the Apache DB - ObjectRelationalBridge Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org