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 Terry Brick <te...@yahoo.com> on 2003/08/25 20:02:23 UTC

Sill need help with references (in a big way)

Okay, I'll try asking again, only this time there are
more questions probably because I never figured out
answers for the first ones I posted.  It's kind of
long because I'm trying to be detailed, if anyone has
any clues to any of the questions, that would help me
out a great deal :)

I'm using OJB 1.0rc4, MySQL, SequenceManagerNativeImpl
for the sequence manager and am using
PersistanceBroker.

#1
---
Whenever I try to store an object that has references
to another class, OJB is requireing that the
referenced class have an autoincrement field.  All I
do to make it work is set autoincrement="true" and
it's fine... even though it may NOT be an auto
incrmement field.  SO FAR, this has not been a big
issue, but I can't help but think it's going to back
and bite me.  I've posted the error message before, I
can go back an retrieve it if someone is interested in
seeing it.

#2
---
This one has been a huge pain for me since I've
started using OJB.  I have a feeling that I'm just
going about things the wrong way and am not
understanding key concepts.  So here's the story (XML
sample at end)...
For example, I may have 3 tables/classes defined in my
repository.  ClassA has a collection of ClassB's and a
reference to ClassD.  So naturally ClassB has a
reference to ClassA.  ClassB also has a reference to
ClassC (I don't know that this last one is important
for this discussion, but just trying emulate my
actualy situation in case it is).
Now say I have a ClassA instance that I want to
persist in the database.  This ClassA instance also
has a collection (Vector) of 5 ClassB's.  In these 5
ClassB's, all the properties are populated except for
TableA_ID because that will not be available until
after this ClassA has been inserted into the database
and the autoincrement id has been retrieved.
In this ClassA instance I also have a TableD_ID
property value.  So this is where I would typically do
my broker.retrieveAllReferences(obj) call (otherwise
it sets TableD_ID to null).  However, doing this in
the situation described above nulls out my collection!
So it inserts ClassA fine, but no ClassB's get
inserted.
So after several hours of painstaking trial and error,
I finally created a function that recursively loops
through all properties, collections and references and
ONLY calls retrieveReference() on properties that are
not null.  The code is attached below.  Does it make
sense that I'm having to do this or did I go about it
the wrong way??
Okay, so that gets me one step further... to my next
problem.  So now after running the code on the object
that retrieves references only for those properties
that are not null, it no longer nulls out my ClassB
collection and OJB actually tries to insert them. 
However, it's trying to insert the ClassB with null
TableA_ID values (these need to be populated with the
ID of the newly created ClassA).  The only way I've
been able to get this to work (what I consider a
temporary workaround) is to REMOVE the reference
descriptor in ClassB that is pointing to ClassA.  This
seems to fix that problem... but I have a feeling I'll
need that ref descriptor back at some point in the
near future.  If none of this makes any sense, please
refer the XML below and I placed a comment in there.

What am I doing wrong?  Please help, I'm going crazy
here.

Thank you!


repository.xml example
========================
<class-descriptor class="ClassA" table="TableA">
  <field-descriptor 
  	name="TableA_ID" 
  	column="TableA_ID" 
  	jdbc-type="BIGINT" 
  	access="readonly" 
  	primarykey="true" 
  	autoincrement="true" 
  	required="true"/>
  <field-descriptor 
  	name="TableD_ID" 
  	column="TableD_ID" 
  	jdbc-type="BIGINT" 
  	required="true"/>
  <reference-descriptor 
  	name="TableD_Ref" 
  	class-ref="ClassD" 
  	auto-retrieve="false">
     <foreignkey field-ref="TableD_ID"/>
  </reference-descriptor>
  <collection-descriptor 
  	name="TablesB" 
  	element-class-ref="ClassB" 
  	auto-retrieve="false" 
  	auto-update="true" 
  	auto-delete="false">
     <inverse-foreignkey field-ref="TableA_ID"/>
  </collection-descriptor>
</class-descriptor>


<class-descriptor class="ClassB" table="TableB">
  <field-descriptor 
  	name="TableB_ID" 
  	column="TableB_ID" 
  	jdbc-type="BIGINT" 
  	access="readonly" 
  	primarykey="true" 
  	autoincrement="true" 
  	required="true"/>
  <field-descriptor 
  	name="TableA_ID" 
  	column="TableA_ID" 
  	jdbc-type="BIGINT" 
  	required="true"/>
  <field-descriptor 
  	name="TableC_ID" 
  	column="TableC_ID" 
  	jdbc-type="BIGINT" 
  	required="true"/>
  <!-- 
  THINGS ONLY WORK IF I TAKE THIS REFERENCE-DESCRIPTOR
OUT.
  If I don't take it out, then OJB tries to insert the
"TablesB" collection elements
  with null TableA_ID values.
  -->
  <reference-descriptor 
  	name="TableA_Ref" 
  	class-ref="ClassA" 
  	auto-retrieve="false">
     <foreignkey field-ref="TableA_ID"/>
  </reference-descriptor>
  <reference-descriptor 
  	name="TableC_Ref" 
  	class-ref="ClassC" 
  	auto-retrieve="false">
     <foreignkey field-ref="TableC_ID"/>
  </reference-descriptor>
</class-descriptor>


<class-descriptor class="ClassC" table="TableC">
  <field-descriptor 
  	name="TableC_ID" 
  	column="TableC_ID" 
  	jdbc-type="BIGINT" 
  	access="readonly" 
  	primarykey="true" 
  	autoincrement="true" 
  	required="true"/>
</class-descriptor>

<class-descriptor class="ClassD" table="TableD">
  <field-descriptor 
  	name="TableD_ID" 
  	column="TableD_ID" 
  	jdbc-type="BIGINT" 
  	access="readonly" 
  	primarykey="true" 
  	autoincrement="true" 
  	required="true"/>
</class-descriptor>


code sample
=============================
/**
* There are two calls to other custom methods 
* getReferenceDescriptors() and
getCollectionDescriptors()
* but they're self explanatory.
*/
public void retrieveNullReferences(Object obj) throws
Exception
{			
	ObjectReferenceDescriptor[] refs =
this.getReferenceDescriptors(obj.getClass());
	for (int i = 0; i < refs.length; i++)
	{
		String fieldName =
refs[i].getPersistentField().getName();
		if (PropertyUtils.getProperty(obj,fieldName) ==
null)
		{
			broker.retrieveReference(obj,fieldName);
		}
		else
		{
		
retrieveNullReferences(PropertyUtils.getProperty(obj,fieldName));
		}
	}
	CollectionDescriptor[] cols =
this.getCollectionDescriptors(obj.getClass());
	for (int i = 0; i < cols.length; i++)
	{
		String fieldName =
cols[i].getPersistentField().getName();
		if (BeanUtils.getProperty(obj,fieldName) == null)
		{
			broker.retrieveReference(o,fieldName);
		}
		else
		{
			logger.debug("trying to cast " + fieldName);
			List list = (List)
PropertyUtils.getProperty(obj,fieldName);
			Iterator it = list.iterator();
			while (it.hasNext())
			{
				retrieveNullReferences(it.next());
			}
		}
	}
}
	
	


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

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


Re: Sill need help with references.. anyone???

Posted by Terry Brick <te...@yahoo.com>.
Thanks for the response... You gave me a few things to
look into, I'll post the results and figure out my
email format... i agree it's annoying trying to read
that.

--- Brian McCallister
<mc...@forthillcompany.com> wrote:
> Most of the core developers on OJB are on vacation
> at the moment (it is 
> August, and OJB has a largely European set of
> developers).
> 
> I will take a stab though (disclaimer, I am just a
> user and part time 
> OJB doc writer).


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

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


Re: Sill need help with references.. anyone???

Posted by Brian McCallister <mc...@forthillcompany.com>.
Most of the core developers on OJB are on vacation at the moment (it is 
August, and OJB has a largely European set of developers).

I will take a stab though (disclaimer, I am just a user and part time 
OJB doc writer).

On Thursday, August 28, 2003, at 02:37 PM, Terry Brick wrote:
>> I'm using OJB 1.0rc4, MySQL,
>> SequenceManagerNativeImpl
>> for the sequence manager and am using
>> PersistanceBroker.
>>

I have never used it with MySQL, uh oh.

>> #1
>> ---
>> Whenever I try to store an object that has
>> references
>> to another class, OJB is requireing that the
>> referenced class have an autoincrement field.  All I

This is required for optimistic locking (alternately I believe a date 
or timestamp field works for this), but shouldn't be required for all 
references without use of optimistic locks. It *does* require a 
primary-key field (I believe, am running from memory here and I always 
have a primary-key specified anyway) which, as the case may be, is 
always auto-incremented. I need to check and verify if auto-increment 
is required for any relation, but that doesn't sound right...

>> do to make it work is set autoincrement="true" and
>> it's fine... even though it may NOT be an auto
>> incrmement field.  SO FAR, this has not been a big
>> issue, but I can't help but think it's going to back
>> and bite me.  I've posted the error message before,
>>
<snip>

It looks like you are using database field names your 
reference-descriptors and collection-descriptors. These should be java 
object fields, not database fields.

Take a look at the 
http://db.apache.org/ojb/howto-use-anonymous-keys.html anonymous key 
tutorial (I think the code -> metadata is clearer there) at the 
class-descriptors.

Any chance you can post your class code? If not I can whip together a 
demo of a working on doing approximately what you ask next week (am 
leaving for a short vacation myself tonight). Sorry no one has gotten 
back to you more quickly.

-Brian

ps: any way you can turn off the line chopping in your mua? It makes 
your mail difficult to read.



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


Re: Sill need help with references.. anyone???

Posted by Terry Brick <te...@yahoo.com>.
am I doomed with OJB or is there some help out there?

--- Terry Brick <te...@yahoo.com> wrote:
> Okay, I'll try asking again, only this time there
> are
> more questions probably because I never figured out
> answers for the first ones I posted.  It's kind of
> long because I'm trying to be detailed, if anyone
> has
> any clues to any of the questions, that would help
> me
> out a great deal :)
> 
> I'm using OJB 1.0rc4, MySQL,
> SequenceManagerNativeImpl
> for the sequence manager and am using
> PersistanceBroker.
> 
> #1
> ---
> Whenever I try to store an object that has
> references
> to another class, OJB is requireing that the
> referenced class have an autoincrement field.  All I
> do to make it work is set autoincrement="true" and
> it's fine... even though it may NOT be an auto
> incrmement field.  SO FAR, this has not been a big
> issue, but I can't help but think it's going to back
> and bite me.  I've posted the error message before,
> I
> can go back an retrieve it if someone is interested
> in
> seeing it.
> 
> #2
> ---
> This one has been a huge pain for me since I've
> started using OJB.  I have a feeling that I'm just
> going about things the wrong way and am not
> understanding key concepts.  So here's the story
> (XML
> sample at end)...
> For example, I may have 3 tables/classes defined in
> my
> repository.  ClassA has a collection of ClassB's and
> a
> reference to ClassD.  So naturally ClassB has a
> reference to ClassA.  ClassB also has a reference to
> ClassC (I don't know that this last one is important
> for this discussion, but just trying emulate my
> actualy situation in case it is).
> Now say I have a ClassA instance that I want to
> persist in the database.  This ClassA instance also
> has a collection (Vector) of 5 ClassB's.  In these 5
> ClassB's, all the properties are populated except
> for
> TableA_ID because that will not be available until
> after this ClassA has been inserted into the
> database
> and the autoincrement id has been retrieved.
> In this ClassA instance I also have a TableD_ID
> property value.  So this is where I would typically
> do
> my broker.retrieveAllReferences(obj) call (otherwise
> it sets TableD_ID to null).  However, doing this in
> the situation described above nulls out my
> collection!
> So it inserts ClassA fine, but no ClassB's get
> inserted.
> So after several hours of painstaking trial and
> error,
> I finally created a function that recursively loops
> through all properties, collections and references
> and
> ONLY calls retrieveReference() on properties that
> are
> not null.  The code is attached below.  Does it make
> sense that I'm having to do this or did I go about
> it
> the wrong way??
> Okay, so that gets me one step further... to my next
> problem.  So now after running the code on the
> object
> that retrieves references only for those properties
> that are not null, it no longer nulls out my ClassB
> collection and OJB actually tries to insert them. 
> However, it's trying to insert the ClassB with null
> TableA_ID values (these need to be populated with
> the
> ID of the newly created ClassA).  The only way I've
> been able to get this to work (what I consider a
> temporary workaround) is to REMOVE the reference
> descriptor in ClassB that is pointing to ClassA. 
> This
> seems to fix that problem... but I have a feeling
> I'll
> need that ref descriptor back at some point in the
> near future.  If none of this makes any sense,
> please
> refer the XML below and I placed a comment in there.
> 
> What am I doing wrong?  Please help, I'm going crazy
> here.
> 
> Thank you!
> 
> 
> repository.xml example
> ========================
> <class-descriptor class="ClassA" table="TableA">
>   <field-descriptor 
>   	name="TableA_ID" 
>   	column="TableA_ID" 
>   	jdbc-type="BIGINT" 
>   	access="readonly" 
>   	primarykey="true" 
>   	autoincrement="true" 
>   	required="true"/>
>   <field-descriptor 
>   	name="TableD_ID" 
>   	column="TableD_ID" 
>   	jdbc-type="BIGINT" 
>   	required="true"/>
>   <reference-descriptor 
>   	name="TableD_Ref" 
>   	class-ref="ClassD" 
>   	auto-retrieve="false">
>      <foreignkey field-ref="TableD_ID"/>
>   </reference-descriptor>
>   <collection-descriptor 
>   	name="TablesB" 
>   	element-class-ref="ClassB" 
>   	auto-retrieve="false" 
>   	auto-update="true" 
>   	auto-delete="false">
>      <inverse-foreignkey field-ref="TableA_ID"/>
>   </collection-descriptor>
> </class-descriptor>
> 
> 
> <class-descriptor class="ClassB" table="TableB">
>   <field-descriptor 
>   	name="TableB_ID" 
>   	column="TableB_ID" 
>   	jdbc-type="BIGINT" 
>   	access="readonly" 
>   	primarykey="true" 
>   	autoincrement="true" 
>   	required="true"/>
>   <field-descriptor 
>   	name="TableA_ID" 
>   	column="TableA_ID" 
>   	jdbc-type="BIGINT" 
>   	required="true"/>
>   <field-descriptor 
>   	name="TableC_ID" 
>   	column="TableC_ID" 
>   	jdbc-type="BIGINT" 
>   	required="true"/>
>   <!-- 
>   THINGS ONLY WORK IF I TAKE THIS
> REFERENCE-DESCRIPTOR
> OUT.
>   If I don't take it out, then OJB tries to insert
> the
> "TablesB" collection elements
>   with null TableA_ID values.
>   -->
>   <reference-descriptor 
>   	name="TableA_Ref" 
>   	class-ref="ClassA" 
>   	auto-retrieve="false">
>      <foreignkey field-ref="TableA_ID"/>
>   </reference-descriptor>
>   <reference-descriptor 
>   	name="TableC_Ref" 
>   	class-ref="ClassC" 
>   	auto-retrieve="false">
>      <foreignkey field-ref="TableC_ID"/>
>   </reference-descriptor>
> </class-descriptor>
> 
> 
> <class-descriptor class="ClassC" table="TableC">
>   <field-descriptor 
>   	name="TableC_ID" 
>   	column="TableC_ID" 
>   	jdbc-type="BIGINT" 
>   	access="readonly" 
>   	primarykey="true" 
>   	autoincrement="true" 
>   	required="true"/>
> </class-descriptor>
> 
> <class-descriptor class="ClassD" table="TableD">
>   <field-descriptor 
>   	name="TableD_ID" 
>   	column="TableD_ID" 
>   	jdbc-type="BIGINT" 
>   	access="readonly" 
>   	primarykey="true" 
>   	autoincrement="true" 
>   	required="true"/>
> </class-descriptor>
> 
=== message truncated ===


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

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


Re: Sill need help with references (in a big way)

Posted by Terry Brick <te...@yahoo.com>.
Sorry, I have an amendment to my previously posted
message.  
The code sample I provided "retrieveNullReferences()"
calls broker.retrieveReference() on
ReferenceDescriptors and CollectionDescriptors that
ARE null.  I think I stated in my previous message
that it called broker.retrieveReference() where they
are NOT null.


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

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