Hi,
I am trying to map the composite pattern with persistent objects.
I have defined 3 classes :
- Element : the component, abstract base class
- Category : the composite
- Item : a leaf
I map concrete classes (Category and Item) in a MySQL data base in the
folowing tables.
CREATE TABLE `category` (
`id` int(3) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
`id_category` int(10) unsigned default '0',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `id_2` (`id`)
) TYPE=MyISAM;
CREATE TABLE `item` (
`id` int(3) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
`id_category` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `id_2` (`id`)
) TYPE=MyISAM;
When I remove a category that contains only items, it works fine : it
deletes the category and the contained items.
When I remove a category that contains items and empty category, it
works fine too : it deletes the category and the contained elements.
But when I try to delete a category that contains category that contains
elements, it does not recurse into the tree structure.
database.begin();
OQLQuery oq;
QueryResults elements;
oq = database.getOQLQuery("SELECT c from c IN " + p_className +
" WHERE Id=$1"); // Select the Composite
oq.bind(p_id);
elements = oq.execute();
Element element = (Element)elements.next();
OQLQuery o = database.getOQLQuery("SELECT c from c IN
com.eurelis.webtools.catalog.Category WHERE Id=$1"); // Select the
Component
o.bind(element.getIdCategory());
QueryResults categories = o.execute();
Category category = (Category)categories.next();
category.removeElement(element);
database.remove(element);
database.commit();
Thanks for your help,
Best Regards,
Vincent
DATABASE.XML :
<database name="db_catalogue" engine="mysql">
<jndi name="java:comp/env/jdbc/ds" />
<mapping href="mapping.xml" />
</database>
========================================================================
==========================
MAPPING.XML
<mapping>
<key-generator name="IDENTITY"/>
<class name="com.eurelis.webtools.catalog.Category"
identity="Id" key-generator="IDENTITY">
<description>Category definition</description>
<map-to table="category"/>
<field name="Id" type="string">
<sql name="id" type="integer"/>
</field>
<field name="IdCategory" type="string">
<sql name="id_category" type="integer"/>
</field>
<field name="Name" type="string" >
<sql name="name" type="varchar"/>
</field>
<field name="Items"
type="com.eurelis.webtools.catalog.Item" collection="vector">
<sql name="id" many-table="item"
many-key="id_category"/>
</field>
<field name="Categories"
type="com.eurelis.webtools.catalog.Category" collection="vector">
<sql name="id" many-table="category"
many-key="id_category"/>
</field>
</class>
<class name="com.eurelis.webtools.catalog.Item" identity="Id"
key-generator="IDENTITY">
<description>Item definition</description>
<map-to table="item"/>
<field name="Id" type="string">
<sql name="id" type="integer"/>
</field>
<field name="IdCategory" type="string">
<sql name="id_category" type="integer"/>
</field>
<field name="Name" type="string" >
<sql name="name" type="varchar"/>
</field>
</class>
</mapping>
========================================================================
==========================
ELEMENT.JAVA
public class Element
{
private String _id = new String();
private String _idCategory = new String("0");
public String getId() { return _id; }
public void setId(String id) { _id = id; }
public String getIdCategory() { return _idCategory; }
public void setIdCategory(String idCategory) { _idCategory =
idCategory; }
public String toString() {
return new String("Element(" + getIdCategory() + ":" + getId() + ")");
}
}
========================================================================
==========================
CATEGORY.JAVA
public class Category extends Element
{
private String _name = new String();
private Vector _categories = new Vector();
private Vector _items = new Vector();
public String getName() { return _name; }
public void setName(String name) { _name = name; }
public Vector getCategories() { return _categories; }
public void setCategories(Vector categories) { _categories =
categories; }
public Vector getItems() { return _items; }
public void setItems(Vector items) { _items = items; }
public void addElement(Element element) {
element.setIdCategory(this.getId());
if(element instanceof Category) {
_categories.add(element); }
else { _items.add(element); }
}
public void removeElement(Element element) {
if(element instanceof Category) {
_categories.remove(element); }
else { _items.remove(element); }
}
public Vector getElements() {
Vector v = new Vector();
v.addAll(getCategories());
v.addAll(getItems());
return v;
}
public String toString() {
return new String("Category(" + getIdCategory() + ":" +
getId() + "," + getName() + "," + getCategories().size() + "," +
getItems().size() + ")");
}
}
========================================================================
==========================
ITEM.JAVA
public class Item extends Element
{
private String _name = new String();
public String getName() { return _name; }
public void setName(String name) { _name = name; }
public String toString() {
return new String("Item(" + getIdCategory() + ":" +
getId() + "," + getName() + ")");
}
}
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev