I think the problem is that you want to create the user as a role, and the user is not a role, I think he is more suitable to say that the auditor is a role (inheritance) and the User has a role (composition), therefore maybe you need to have common user properties in one object, and the role table where you need to abstract your inheritance
Something like that
@Entity @Table(name = "user") public class User{
And here your entity role with will be the parent class
@Entity @Inheritance(strategy = InheritanceType.JOINED) public abstract class Role implements Serializable { @SequenceGenerator(name = "ROLE_SEQ", sequenceName = "ROLE_SEQ", allocationSize = 1) @GeneratedValue(generator = "ROLE_SEQ") @Id private Long idRole; @OneToOne(optional = false) private User user;
}
and your specific role
@Entity @Table(name = "auditor") public class Auditor extends Role {
So you can do something like this in java
User user = new User(); //set all the properties that you need for user or the reference for an existing user . . //Set the role that you need Auditor auditor = new Auditor(); auditor.setUser(user); user.setRole(auditor); userDAO.insert(user);
source share