您可以在索引鍵類別加入一個關係角色。
新增的關係角色必須有下列性質:
另外,索引鍵要新增其中的 Enterprise Bean 必須是根 Bean(不是繼承自某個超類型)。因為繼承的 Bean 必須使用超類型的主鍵類別,因此,索引鍵類別的外觀不能改變。
下列作業參照兩個 Enterprise Bean:一個是包含角色的 Enterprise Bean(這個 Bean),一個是角色中所代表的 Enterprise Bean(其他 Bean)。
這個動作會將其他 Bean 的索引鍵欄位新增至這個 Bean 的索引鍵類別和 Bean 類別中。在新增角色至主鍵時,也會重新產生關聯。
當您從關聯中移除 Bean 時,請特別小心。 如果您已新增其中一個角色至其中一個 Bean 的索引鍵,就必須先移除含有索引鍵中的角色之 Bean,再移除其他 Bean。資料庫限制可強制採用這個作法(即使角色不是索引鍵的一部分也一樣)。不過,如果沒有資料庫限制,仍會發生問題。 比方說,假設 1:1 關聯中有 Customer 和 Address Bean, 其中 Address Bean 的 customer 角色包含外來鍵,而 customer 角色則是 Address Bean 的主鍵。如果您移除了 Customer Bean,但沒有先移除 Address Bean,當您試圖將 Address Bean 的 customer 角色外來鍵設為空值時, 關聯維護碼也會嘗試將 Address Bean 的主鍵設為空值。