Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g (10.1.3.5.0) Part Number E13981-01 |
|
|
View PDF |
Two entities–an owning (parent or source) entity and an owned (child or target) entity–are related by aggregation if there is a strict one-to-one relationship between them and all the attributes of the owned entity can be retrieved from the same table(s) as the owning entity. This means that if the owning entity exists, then the owned entity must also exist and if the owning entity is destroyed, then the owned entity is also destroyed.
An aggregate mapping lets you associate data members in the owned entity with fields in the owning entity's underlying database tables.
In the owning entity, you designate the owned field or setter as embedded.In owned entity, you designate the class as embeddable and associate it with the owning entity's table name.
In the owning entity, you can override any column specifications (see "Configuring a Column") made in the owned entity.
For more information, see "Understanding Aggregate Mapping" in the Oracle TopLink Developer's Guide.
Note:
For an EJB 3.0 basic mapping code example, see:http://www.oracle.com/technology/tech/java/oc4j/ejb3/howtos-ejb3/howtoejb30mappingannotations/doc/how-to-ejb30-mapping-annotations.html#embedded
.Example 7-24 shows how to use the @Embedded
annotation to specify an aggregate mapping for field period
. This field contains an instance of EmploymentPeriod
. Example 7-25 shows how to use the @Embeddable
annotation to specify the EmploymentPeriod
entity class as being eligible for use in an aggregate mapping and how to use the @Table
annotation (see "Configuring the Primary Table") to associate this class with the owning entity's table.
Example 7-24 @Embedded
@Entity @Table(name="EJB_EMPLOYEE") public class Employee implements Serializable { ... @Embedded public EmploymentPeriod getPeriod() { return period; } ... }
Example 7-25 @Embeddable
@Embeddable @Table(name="EJB_EMPLOYEE") public class EmploymentPeriod implements Serializable { private Date startDate; private Date endDate; ... }
You can use the @AttributeOverride
in the owning entity (see Example 7-26) to override the column definitions made in the owned entity (see Example 7-27).
Example 7-26 @Embedded and @AttributeOverride
@Entity @Table(name="EJB_EMPLOYEE") public class Employee implements Serializable { ... @Embedded({ @AttributeOverride(name="startDate", column=@Column("EMP_START")), @AttributeOverride(name="endDate", column=@Column("EMP_END"))} ) public EmploymentPeriod getPeriod() { return period; } ... }