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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]