Hello,
I have other parts using many-many relations, but this one doesn't run
correctly. I've tried all and doesn't see what is wrong.
When the relation table is directly populated on the database, Castor
loads correctly the inner List of my object . upon a store.
I have added a dummy single property which is correctly handled. But the
inner List correctly set during the transaction and correctly visible in
the in memory object is lost when the same object is new required. So
the list comes always empty (if I don't have set manually in the
database some relations).
Here are the main elements of the problem.
Thanks if someone can see what is wrong.
Jean-Pierre PAWLAK
<!-- Mapping ITEMS ==============================================
-->
<!-- ============================================================
-->
<class name="com.achappro.entreprise.object.Item"
identity="id" key-generator="IDENTITY">
<description>Articles</description>
<map-to table="ach_items" xml="item" />
<field name="id" type="integer">
<sql name="item_id" type="integer"/>
<xml name="id" node="attribute"/>
</field>
<field name="manufacturerId" type="integer">
<sql name="manufacturer_id" type="integer"/>
<xml name="manufacturer-id" node="element"/>
</field>
<field name="familyId" type="integer">
<sql name="family_id" type="integer"/>
<xml name="family-id" node="element"/>
</field>
<field name="equivId" type="integer">
<sql name="equiv_id" type="integer"/>
<xml name="equiv-id" node="element"/>
</field>
<field name="vatType" type="string">
<sql name="vattype" type="char" />
<xml name="vat-type" node="element" />
</field>
<field name="defaultName" type="string">
<sql name="default_name" type="varchar" />
<xml name="default-name" node="element" />
</field>
<field name="defaultDescription" type="string">
<sql name="default_description" type="varchar" />
<xml name="default-description" node="element" />
</field>
<field name="manufacturerRef" type="string">
<sql name="manufacturer_ref" type="varchar" />
<xml name="manufacturer-ref" node="element" />
</field>
<field name="document" type="string">
<sql name="link" type="varchar" />
<xml name="document" node="element" />
</field>
<field name="locItems" type="com.achappro.entreprise.object.LocItem"
collection="arraylist">
<sql many-table="ach_locitems" many-key="item_id" />
<xml name="loc-items" node="element" />
</field>
<!--
<field name="itemCategories"
type="com.achappro.entreprise.object.ItemCategory"
collection="arraylist">
<sql name="category_id" many-table="ach_item_categories"
many-key="item_id" />
<xml name="item-categories" node="element" />
</field>
-->
</class>
<!-- Mapping CATEGORIES =========================================
-->
<!-- ============================================================
-->
<class name="com.achappro.entreprise.object.ItemCategory"
identity="id" key-generator="IDENTITY">
<description>Categories d'articles</description>
<map-to table="ach_categories" xml="item-category" />
<field name="id" type="integer">
<sql name="category_id" type="integer"/>
<xml name="id" node="attribute"/>
</field>
<field name="parentId" type="integer">
<sql name="parent_id" type="integer"/>
<xml name="parent-id" node="element"/>
</field>
<field name="defaultName" type="string">
<sql name="default_name" type="varchar" />
<xml name="default-name" node="element" />
</field>
<field name="defaultDescription" type="string">
<sql name="default_description" type="varchar" />
<xml name="default-description" node="element" />
</field>
<field name="code" type="string">
<sql name="code" type="varchar" />
<xml name="code" node="element" />
</field>
<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" />
<xml name="items" node="element" />
</field>
<field name="locCategories"
type="com.achappro.entreprise.object.LocItemCategory"
collection="arraylist">
<sql many-table="ach_loccategories" many-key="category_id" />
<xml name="loc-categories" node="element" />
</field>
<field name="dummy" type="integer">
<sql name="dummy" type="integer"/>
<xml name="dummy" node="element"/>
</field>
</class>
public class ItemCategory extends EntrepriseLongTransObject {
. . . .
private static org.apache.log4j.Category log =
org.apache.log4j.Category.getInstance(ItemCategory.class.getName());
private Integer id;
private Integer parentId;
private String defaultName;
private String defaultDescription;
private String code;
private String name;
private String description;
private List locCategories = new ArrayList();
private Map locales = new HashMap();
private List items = new ArrayList();
private int dummy = 0;
public void addLocCategories(LocItemCategory locCategory) {
if ( ! locCategories.contains(locCategory)) {
locCategories.add(locCategory);
locCategory.setItemCategoryId( this.id);
locales.put( locCategory.getLang(), locCategory);
}
}
public void removeLocCategories(LocItemCategory locCategory) {
if ( locCategories.contains(locCategory)) {
locCategories.remove(locCategory);
locales.remove( locCategory.getLang());
}
}
public void setLocCategories(int idx, LocItemCategory locCategory)
{
if ( idx <= locCategories.size()) {
locCategories.set(idx, locCategory);
locCategory.setItemCategoryId( this.id);
locales.put( locCategory.getLang(), locCategory);
}
}
protected void localize(Locale locale) {
super.localize(locale);
. . .
}
// -------------------------------------------------------------
Accesseurs
public String getDescription() {return description;}
public Integer getId() {return id;}
public String getName() {return name;}
public void setDescription(String description) {this.description =
description;}
public void setId(Integer id) {this.id = id;}
public void setName(String name) {this.name = name;}
public void setLocCategories(List locCategories) {
this.locCategories = locCategories;
if (null != locCategories) {
try {
Iterator it = locCategories.iterator();
LocItemCategory locCategory;
while (it.hasNext()) {
locCategory = (LocItemCategory)it.next();
locCategory.setItemCategoryId(this.id);
}
this.locCategories = locCategories;
} catch (Exception e) {
log.error("set locCategories error: " +
e.getMessage());
}
} else {
this.locCategories = locCategories;
}
}
public String getDefaultDescription() {return defaultDescription;}
public String getDefaultName() {return defaultName;}
public List getLocCategories() {return locCategories;}
public void setDefaultDescription(String defaultDescription) {
this.defaultDescription = defaultDescription;
}
public void setDefaultName(String defaultName) {this.defaultName =
defaultName;}
public String getCode() {return code;}
public Integer getParentId() {return parentId;}
public void setCode(String code) {this.code = code;}
public void setParentId(Integer parentId) {this.parentId =
parentId;}
-------------------- The items part is not correctly working
------------------------------
----- (not transferred in the database but correctly set in memory
------------------------
public List getItems() {return items;}
public Item getItems(int index) {return (Item)items.get(index);}
public void setItems(List items) {this.items = items; }
// public void setItems(int index, Item item) {
// this.items.set( index,item);
// }
public void setItems(Item item, int index) {
this.items.set( index,item);
}
// public void setItems(int idx, Item item) {
// if ( idx <= items.size()) {
// items.set(idx, item);
// }
// }
// public void addItems(Item item) {
// if ( ! items.contains(item)) {
// items.add(item);
// item.addItemCategories(this);
// }
// }
public void addItem(Item item) {
if ( ! items.contains(item)) {
items.add(item);
//item.addItemCategories(this);
} else {
log.debug("Item id("+item.getId()+") NOT added, yet in
category "+defaultName);
}
}
public void removeItem(Item item) {
if ( items.contains(item)) {
items.remove(item);
//item.removeItemCategories(this);
}
}
public int getItemsCount() {
return items.size();
}
public int getDummy() {return dummy;}
public void setDummy(int dummy) {this.dummy = dummy;}
}
------------------------------------------------------------------------
--------------------------------------
public void addItem(String code, Item item, ApplicationContext
appContext)
throws EntrepriseException {
Database db = null;
String query;
OQLQuery myOql;
QueryResults results;
ItemCategory cat = 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();
try { Map map = BeanUtils.describe(cat);
log.debug("BEFORE: "+map.toString());} catch(Exception e) {}
// All seems OK, but here items is always empty, I
have no error message and dummy is correctly
// stored
cat.addItem(item);
cat.setDummy(cat.getDummy()+1);
// I tried this, but without success
db.commit();
db.begin();
db.update(cat);
db.commit();
// Here I can see the item set which will not be retrieved in the
database.
try { Map map = BeanUtils.describe(cat);
log.debug("AFTER: "+map.toString());} catch(Exception e) {}
log.debug("committed");
} catch (Exception e) {
handleException(db, e,cat,"addItem
error","dberror.find");
} finally { close(db); }
}
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev