blob: 9fcb65c770ade572942e7c6016c30f00ef698c5e [file] [log] [blame]
Title: Modeling Embeddables
<P>An <TT>Embeddable</TT> is a persistent Java class that does not have its own persistent &quot;identity&quot; (&quot;identity&quot; is an ORM concept similar to &quot;primary key&quot;) and is managed by Cayenne as a part of its owning object. Embeddables are mapped in Cayenne similar to ObjEntities, except that they are not linked to any DbEntity. An Embeddable becomes linked to one when it is &quot;embedded&quot; in an owning ObjEntity. &quot;Embedding&quot; means mapping ObjEntity's ObjAttribute to an embeddable type. A single embeddable can be included (&quot;embedded&quot;) in more than one ObjEntity.</P>
<P>Embeddable classes are generated by Cayenne (via the Modeler, Ant or Maven), just like other persistent classes. There's no common embeddable superclass, so each embeddable inherits from Object. Still Cayenne generates a familiar pair of <TT>&#95;Xyz</TT> superclass and <TT>Xyz</TT> subclass for each Embeddable.</P>
<H3><A name="ModelingEmbeddables-MappinganEmbeddable"></A>Mapping an Embeddable</H3>
<P>A new Embeddable is created by clicking on the &quot;Create Embeddable&quot; button on the toolbar (or selecting an appropriate item from the menu). The only required field is a fully qualified class name:</P>
<P><SPAN class="image-wrap" style=""><IMG src="modeling-embeddables.data/modeler-newembeddable.png" style="border: 0px solid black"></SPAN></P>
<P>An embeddable can have one or more attributes. Each attribute requires at least the name and the java type specified. Quite often you would also specify the DbAttribute name. Although Embeddable is not connected to a DbEntity just yet, this will save some typing during embedding it to ObjEntities (provided the DB column names for the Embeddable are the same across ObjEntities):</P>
<P><SPAN class="image-wrap" style=""><IMG src="modeling-embeddables.data/modeler-eattributes.png" style="border: 0px solid black"></SPAN></P>
<H3><A name="ModelingEmbeddables-EmbeddinganEmbeddable"></A>Embedding an Embeddable</H3>
<P>The process of embedding an Embeddable is rather simple. Add a new attribute to your Java class (under the ObjEntity Attributes tab) and then inspect the entry. (Note: It is important that you inspect the entry instead of entering the embeddable's type directly in the Java Type column under the Attributes tab due to a limitation in Cayenne Modeler.) Select the embeddable type in the Type pulldown list. CayenneModeler will automatically include all Embeddable classes it knows about in the Java types dropdown. If the underlying DbEntity has the same column names as the original Embeddable, that's all you need to do. If not, you can override the default column names by clicking on the attribute inspector button (&quot;I&quot;) and changing DbAttribute mappings for this particular embedding:</P>
<P><SPAN class="image-wrap" style=""><IMG src="modeling-embeddables.data/modeler-embeddedinspector.png" style="border: 0px solid black"></SPAN></P>