I have the following model - a user entity which has a user profile. Depending on the role of the user, user profile varies. I have tried to model this one to one mapping between user and user profile using the shared primary key as per this link
@Entity public class User implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(updatable = false,nullable = false) @GeneratedValue(strategy = GenerationType.SEQUENCE) private Long userId; private String role; @OneToOne @PrimaryKeyJoinColumn private UserProfile profile; } @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class UserProfile implements Serializable { @Id @Column(unique = true,nullable = false) @GeneratedValue(generator = "gen") @GenericGenerator(name = "gen",strategy = "foreign", parameters = @Parameter(name = "property",value = "user")) private Long userId; @OneToOne(mappedBy = "profile") private User user; } @Entity public class DeviceUserProfile extends UserProfile { private Long deviceId; } @Entity public class AdminUserProfile extends UserProfile { private String firstName; private String lastName; } But when I insert the user entity, the user profile is not being added to the corresponding table.
User user1 = new User(); user1.setRole("Device"); DeviceUserProfile deviceUserProfile = new DeviceUserProfile(); deviceUserProfile.setDeviceId(1234L); user1.setProfile(deviceUserProfile); deviceUserProfile.setUser(user1); session.beginTransaction(); session.save(user1); session.getTransaction().commit(); Any pointers on how to solve this or if my model could be better would be very helpful.