Jump to: navigation, search

Embeddables and Mapped Superclasses in JPA Diagram Editor

Overview

The JPA Diagram Editor enables the JPA developers to create, view, and edit their business data model as graphical diagram. For detailed information, see the project proposal.

This page describes how the behavior and the visualization of the Embeddable classes and Mapped Superclasses must look in the JPA Diagram Editor.

Mapped Superclasses

An entity may inherit from a superclass that provides persistent entity state and mapping information, but which is not itself an entity. Typically, the purpose of such a mapped superclass is to define state and mapping information that is common to multiple entity classes.

To be able to create a new Mapped Superclass in the JPA diagram editor, there shall be a new palette entry under the Entities section of the pallete, named Mapped Superclass:

MpdSprclssPaletteEntry.jpg

The newly added mapped superclass shall be colored green to make it easier to distinguish from the entities in the JPA diagram editor and by default shall not contain any attributes:

MpdSprcls.jpg

In palette of the JPA diagram editor, there shall be a new section, named Inheritance, which shall contain only one palette entry: Inherited Entity:

InheritedEntityPaletteEntry.jpg

After selecting the Inherited Entity, click on the already created mapped superclass and drag somewhere in the diagram, a new inherited entity shall appear in the diagram. (The connection shall look like the inheritance relationship in the UML modeling):

InheritedEntity.jpg

There shall be clear enough that the newly created entity will be the subclass of the mapped superclass, that's why the entity shall be connected to the mapped superclass with a dashed green arrow, indicating that this connection is of type "Is-A" relationship and differs from the other entity's specific relationship mappings available in the palette.

Note: An entity can inherit another entity, so the Inherited Entity feature shall be available to be used for creation of a sublassed entity from another entity.

Relationships defined by a mapped superclass must be unidirectional with an owning side only. That means that only the unidirectional relationships from the palette shall be possible to be created and the direction of the relationships shall be from the mapped superclass to the entity.

OneToOneUniDirMpdSprcl.jpg

Embeddables

An entity may use other fine-grained classes to represent entity state. Instances of these classes, unlike entity instances, do not have persistent identity of their own. Instead, they exist only as part of the state of the entity to which they belong. An entity may have collections of embeddables as well as single-valued embeddable attributes. Embeddables may also be used as map keys and map values. Embedded classes belong strictly to their owning entity, and are not sharable across persistent entities.


To be able to create a new Embeddable class in the JPA diagram editor, there shall be a new palette entry under the Entities section of the palette, named Embeddable:

EmbeddablePaletteEntry.jpg

The newly added embeddable class shall be colored violet to make it easier to distinguish from the entities and mapped superclasses in the JPA diagram editor and by default shall not contain any attributes:

Embeddable.jpg

JPA 1.0 Support

In JPA 1.0 only Basic and Transient mapping were supported. JPA 1.0 does not support nested embeddable classes and relationships from embeddable.

JPA 2.0 Support

In JPA 2.0 the supported mapping types are:

  1. basic
  2. many-to-one
  3. one-to-many
  4. one-to-one
  5. many-to-many
  6. element-collection
  7. embedded
  8. transient

In palette of the JPA diagram editor, there shall be a new section, named Composition, which shall contain two palette entries: Embed Single Object and Embed Collection of Objects:

EmbedReferencesPalette.jpg

Note: In case of JPA 1.0, there shall be section Composition, which shall contain only one palette entry Embed Single Object.

To embed a signle class into the entity, the Embed Single Object palette entry shall be selected and then user shall click once on the entity and once on the embeddable class. As a result a new connection shall appear in the diagram and a new embedded attribute shall be added in the entity. (The connection shall look like the composition aggregation relationship in the UML modeling):

Embedded.jpg

There shall be clear enough that the embeddable class is embedded in the entity, that's why the entity shall be connected to the embeddable class with a dash-dot-dotted violet arrow, indicating that the entity "has a reference" to the embeddable class and it differs from the "Is-A" relationship.

If an embeddable class is embedded in two different entities, the diagram shall look like this:

EmbeddedByTwo.jpg

An embeddable class can be embedded twice by the same entity. In this case the JPA diagram shall look like this:

EmbeddedTwice.jpg

To embed a collection of classes in the entity, the Embed Collection of Objects palette entry shall be selected and the user shall click once on the entity and then once on the embeddable class. As a result a new olive-green dash-dot-dotted connection shall appear in the JPA digram editor and a new embedded attribute, annotated as ElementCollection shall be added in the entity:

EmbeddedCollection.jpg

Note: In JPA 2.0 Embeddable classes can be embedded into another embeddable class, so the "Embed Single Object" and "Embed Collection of Objects" features shall be available in this case too.

Note: Element collections shall not contain embeddables with element collection mappings.

Embeddable classes can define any type of relationships in case of JPA 2.0:

OneToManyUniDirEmbeddable.jpg

Note: If the embeddable class is not embedded, the creation of unidirectional relationships shall not be possible.

In case of bidirectional relationship, the target's inverse relationship attribute shall be back to the embedding entity:

EmbeddedManyToManyBiDirRel.jpg

The generated source code shall look in this way:

@Embeddable 
public class ProjectInfo {
    @ManyToMany
    private List<Project> project;
}
 
@Entity
public class Employee {
    @Id
    private Long id;
    @Embedded
    private ProjectInfo projectInfo;
}
 
@Entity
public class Project {
    @Id
    private Long id;
    @ManyToMany(mappedBy="projectInfo.project")
    private Collection<Employee> employee;
}

Note: If an embeddable is embedded as element-collection, only one-to-one and many-to-one relationships shall be allowed.

Note: Relationships to embeddables shall not be allowed. Generally relationships shall be to the entity, in which the embeddable class is embedded, not to the embeddable itself.

Note: If the embeddable class is not embedded, the creation of bidirectional relationships shall not be possible.