| --- |
| # Licensed to the Apache Software Foundation (ASF) under one |
| # or more contributor license agreements. See the NOTICE file |
| # distributed with this work for additional information |
| # regarding copyright ownership. The ASF licenses this file |
| # to you under the Apache License, Version 2.0 (the |
| # "License"); you may not use this file except in compliance |
| # with the License. You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, |
| # software distributed under the License is distributed on an |
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| # KIND, either express or implied. See the License for the |
| # specific language governing permissions and limitations |
| # under the License. |
| |
| title: CayenneModeler Flattened Relationships |
| url: /docs/1.2/cayennemodeler-flattened-relationships.html |
| layout: docs_legacy |
| --- |
| |
| |
| |
| <H3><A name="CayenneModelerFlattenedRelationships-FlattenedRelationships"></A>Flattened Relationships</H3> |
| |
| <P><A href="http://objectstyle.org/cayenne/api/cayenne/org/objectstyle/cayenne/map/ObjRelationship.html" class="external-link" rel="nofollow">ObjRelationships</A> that map to more than a single <A href="http://objectstyle.org/cayenne/api/cayenne/org/objectstyle/cayenne/map/DbRelationship.html" class="external-link" rel="nofollow">DbRelationship</A> are called "flattened".</P> |
| |
| <P>The most common case for using flattened relationships is modeling many-to-many relationships with a join table. E.g. ARTIST DbEntity has to-many relationship to ARTIST_EXHIBIT; ARTIST_EXHIBIT in turn has a to-one relationship to EXHIBIT DbEntity. We can create a single ObjRelationship from Artist ObjEntity to Exhibit ObjEntity that spawns two DbRelationships. This will be a to-many relationship. Lets say it is called "exhibitArray". Generated class file for Artist would contain a getExhibitArray() method returning a list of Exhibits for a given artist instance. An obvious advantage of having a flattened relationship in this case is to avoid mapping a join table as an object, meaningless in the context of the object model.</P> |
| |
| <P>Flattened relationships work across any combination of to-one and to-many DbRelationships. If all the relationships are to-one, then the resulting flattened relationship is also to-one. If there is at least one to-many, the result is to-many as well.</P> |
| <DIV class="panelMacro"><TABLE class="noteMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><img src="/docs/1.2/images/warning.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD><B>Read-Only Note</B><BR>Most of flattened relationships are treated as read-only by Cayenne. Only one (the most commonly used) type is read/write. It is many-to-many (n:m) flattened relationships with a single join table.</TD></TR></TABLE></DIV> |
| <P>To map an ObjRelationship to a chain of DbRelationships, select ObjRelationship table row and click "Edit Relationship" button in the bottom right. "ObjRelationship Inspector" dialog will appear.</P> |
| |
| <P><SPAN class="image-wrap" style=""><img src="/docs/1.2/images/objrelationship-path-selection.jpg" style="border: 0px solid black"></SPAN></P> |
| |
| <P>The dialog contains a DbRelationship table. Clicking on each row in this table will show valid choices of DbRelationships. Once you add a "chain" component, another row automatically opens below, so you can build a chain of arbitrary length.</P> |
| <DIV class="panelMacro"><TABLE class="tipMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><img src="/docs/1.2/images/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD><B>Join Table Tip</B><BR>If you flatten across a join table and this join table was mapped to an ObjEntity (and a correspoding Java class), such ObjEntity can be deleted, as it is no longer needed. Underlying DbEntity must be preserved.</TD></TR></TABLE></DIV> |