Skip Headers

# Relational Aggregates and Nesting

Previous

Next

TopLink supports nested aggregates. In Figure: Nested Aggregates source class `HockeyPlayer` is a normal nonaggregate class descriptor. It owns target class `Info` which is designated as an aggregate. The `Info` class itself owns target classes `PersonalInfo` and `TeamInfo` which are each designated as aggregates.

Nested Aggregates

Description of "Nested Aggregates"

In EJB 3.0, an aggregate is known as an embeddable. In the EJB 3.0 specification, an embeddable may not contain another embeddable (that is, the EJB 3.0 specification does not support nested aggregates).

However, if you deploy a TopLink-enabled EJB 3.0 application with persistence to OC4J, you can take advantage of a TopLink extension of the EJB 3.0 specification to configure nested embeddables. Note that if you do so, your application will not be strictly EJB 3.0-compliant. Example: Nested Embeddables shows the classes from Figure: Nested Aggregates using EJB 3.0 annotations to take advantage of the TopLink extension of the EJB 3.0 specification to allow `Info` (an embeddable) to own embeddables `TeamInfo` and `PersonalInfo`.

Nested Embeddables

    public class HockeyPlayer implements Serializable {
        private int playerId;
        private Info Info;
        private String lastName;
        private String firstName;
        ...
        @Embedded
        public Info getInfo() {
            return Info;
        }
    }

    @Embeddable
    public class Info implements Serializable {
        TeamInfo teamInfo; // TopLink extension of EJB 3.0 allows Embeddable with Embeddable
        PersonalInfo personalInfo;

        public Info() {}

        @Embedded
        public PersonalInfo getPersonalInfo() {
            return personalInfo;
        }

        public void setPersonalInfo(PersonalInfo personalInfo) {
            this.personalInfo = personalInfo;
        }

        @Embedded
        public TeamInfo getTeamInfo() {
            return teamInfo;
        }

        public void setTeamInfo(TeamInfo teamInfo) {
            this.teamInfo = teamInfo;
        }
    }

    @Embeddable
    public class PersonalInfo implements Serializable {
        private int age;
        private double weight;
        private double height;
        ...
    }

    @Embeddable
    public class TeamInfo implements Serializable {
        private String position;
        private int jerseyNumber;
        private HockeyTeam hockeyTeam;
        ...
    }