Bruce,
Thanks for the answer. But I had only issue with updating the database,
reading from was right. So the proposed mappings don't work at all. The
mapping was right. I discover myself the reason but cannot explain the
deep reasons. The item to add on the collection was previously loaded
out of the current transaction and received as parameter. I changed the
function to have only the item key as parameter and load the item object
to be added on the collection inside the transaction. And now it's good.
I changed the previously addItem function to addItemById in this way.
public void addItemById(
String code,
Integer itemId,
^^^^^^^^^^^^^^^
ApplicationContext appContext)
throws EntrepriseException {
Database db = null;
String query;
OQLQuery myOql;
QueryResults results;
ItemCategory cat = null;
Item item = null;
try {
query = "SELECT o FROM "+ITEMCATEGORY_CLASSNAME+" o
WHERE o.code = $1";
db = getJdo(appContext).getDatabase();
db.begin();
myOql = db.getOQLQuery(query);
myOql.bind(code);
results = myOql.execute();
if (! results.hasMore() ) {
log.error("addItem error ==>
dberror.find.notfound");
throw new
EntrepriseException("dberror.find.notfound");
} else {
cat = (ItemCategory)results.next();
}
myOql.close();
item =
(Item)db.load(Class.forName(ITEM_CLASSNAME),itemId);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cat.addItem(item);
db.commit();
} catch (Exception e) {
handleException(db, e,cat,"addItem
error","dberror.find");
} finally { close(db); }
}
-----Message d'origine-----
De�: Bruce Snyder [mailto:[EMAIL PROTECTED]]
Envoy�: lundi 30 septembre 2002 16:48
��: [EMAIL PROTECTED]
Objet�: Re: [castor-dev] Database not updated for a many-many relation
This one time, at band camp, JP PAWLAK(Tiscali) said:
JP>I have other parts using many-many relations, but this one doesn't
run
JP>correctly. I've tried all and doesn't see what is wrong.
JP>When the relation table is directly populated on the database, Castor
JP>loads correctly the inner List of my object . upon a store.
JP>
JP>I have added a dummy single property which is correctly handled. But
the
JP>inner List correctly set during the transaction and correctly visible
in
JP>the in memory object is lost when the same object is new required. So
JP>the list comes always empty (if I don't have set manually in the
JP>database some relations).
JP>
JP>
JP>Here are the main elements of the problem.
JP>Thanks if someone can see what is wrong.
JP>
...
JP> <class name="com.achappro.entreprise.object.Item"
JP> identity="id" key-generator="IDENTITY">
JP> <description>Articles</description>
...
JP> <field name="itemCategories"
JP>type="com.achappro.entreprise.object.ItemCategory"
JP>collection="arraylist">
JP> <sql name="category_id" many-table="ach_item_categories"
JP>many-key="item_id" />
JP> <xml name="item-categories" node="element" />
JP> </field>
JP> -->
JP> </class>
...
JP> <class name="com.achappro.entreprise.object.ItemCategory"
JP> identity="id" key-generator="IDENTITY">
JP> <description>Categories d'articles</description>
...
JP> <field name="items" type="com.achappro.entreprise.object.Item"
JP>required="true" collection="arraylist">
JP> <sql name="item_id" many-table="ach_item_categories"
JP>many-key="category_id" />
JP> <xml name="items" node="element" />
JP> </field>
...
Jean-Pierre,
It seems to me that you have your many-keys reversed. They should be
changed from this:
<field name="itemCategories"
type="com.achappro.entreprise.object.ItemCategory"
collection="arraylist">
<sql name="category_id" many-table="ach_item_categories"
many-key="item_id" />
^^^^^^^^^^^
<field name="items" type="com.achappro.entreprise.object.Item"
required="true"
collection="arraylist">
<sql name="item_id" many-table="ach_item_categories"
many-key="category_id" />
^^^^^^^
to this:
<field name="itemCategories"
type="com.achappro.entreprise.object.ItemCategory"
collection="arraylist">
<sql name="item_id" many-table="ach_item_categories"
many-key="item_id" />
^^^^^^^
<field name="items" type="com.achappro.entreprise.object.Item"
required="true"
collection="arraylist">
<sql name="category_id" many-table="ach_item_categories"
many-key="category_id" />
^^^^^^^^^^^
The many-keys should always reference the identity of the object in
which they're contained.
Bruce
--
perl -e 'print
unpack("u30","<0G)U8V4\@4VYY9&5R\"F9E<G)E=\$\!F<FEI+F-O;0\`\`");'
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev