Question

I'm new with Apache Cayenne.

I have only one Entity, called Product. This entity has a many-to-many relationship with itself, that is a product can contain products, and it can be contained by other products.

I can't model this relationship with Cayenne.. What I do is: 1) I create a table called Composition, with two fields that are both PKs and FKs. 2) I create two toMany from Product to Composition (one from product.id to Composition.contained_id, and one from product.id to Composition.base_id) This should work with the DB Now I create only one ObjEntity: Product. But.. How can I create a flattened relationship?? I'm following this: http://cayenne.apache.org/doc/cayennemodeler-flattened-relationships.html but maybe because it is a relationship with itself I cannot select an Entity in "Target" combo box..

Thank you Francesco

EDIT: the target checkbox problem there is also if the two entities are different. Cayenne Modeler v.3.0.2

Était-ce utile?

La solution

"Target" combo is empty when you select the first relationship, simply because there's no ObjEntity for the join table. But if you keep selecting the next path component, "Product" will appear in the combobox. I wish we redesign this UI for better clarity, but it still works now. See the DataMap XML sample below. I just created it with 3.0.2 Modeler.

Hope this helps.

<?xml version="1.0" encoding="utf-8"?>
<data-map xmlns="http://cayenne.apache.org/schema/3.0/modelMap"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://cayenne.apache.org/schema/3.0/modelMap http://cayenne.apache.org/schema/3.0/modelMap.xsd"
  project-version="3.0.0.1">
    <db-entity name="composition">
        <db-attribute name="BASE_ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
        <db-attribute name="CONTAINED_ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
    </db-entity>
    <db-entity name="product">
        <db-attribute name="ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
        <db-attribute name="NAME" type="VARCHAR" length="255"/>
    </db-entity>
    <obj-entity name="Product" dbEntityName="product">
        <obj-attribute name="name" type="java.lang.String" db-attribute-path="NAME"/>
    </obj-entity>
    <db-relationship name="base" source="composition" target="product" toMany="false">
        <db-attribute-pair source="BASE_ID" target="ID"/>
    </db-relationship>
    <db-relationship name="contained" source="composition" target="product" toMany="false">
        <db-attribute-pair source="CONTAINED_ID" target="ID"/>
    </db-relationship>
    <db-relationship name="base" source="product" target="composition" toDependentPK="true" toMany="true">
        <db-attribute-pair source="ID" target="BASE_ID"/>
    </db-relationship>
    <db-relationship name="contained" source="product" target="composition" toDependentPK="true" toMany="true">
        <db-attribute-pair source="ID" target="CONTAINED_ID"/>
    </db-relationship>
    <obj-relationship name="base" source="Product" target="Product" deleteRule="Deny" db-relationship-path="contained.base"/>
    <obj-relationship name="contained" source="Product" target="Product" deleteRule="Deny" db-relationship-path="base.contained"/>
</data-map>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top