@Entity public class Profile implements Serializable { private static final long serialVersionUID = 1L; }
@Embeddable public class ContactInfo implements Serializable { private static final long serialVersionUID = 1L; public String email; public String address; }
@Entity public class User implements Serializable { private static final long serialVersionUID = 1L; @Id public int userId; // Note: It is valid to have the user info data values be null for a given profile @ElementCollection @CollectionTable(name = "USER_INFO", joinColumns = @JoinColumn(name = "USER_ID")) @MapKeyJoinColumn(name = "PROFILE_ID") public Map<Profile, ContactInfo> contactInfo; }
With the schema above, after the initial insert where the related ContactInfo data for a given profile contains null except for the key attributes (user_id, profile_id):
-
Reads fail to retrieve the row for the profile with null attributes
-
The use of the merge(Entity) API for User entities may result in duplicate insert attempts
-
The use of the saveOrUpdate(Entity) API for User entities may result in deletion of all rows for a given user followed by re-insertion (rather than an update)
|