self referencing object in JPA
- by geoaxis
Hello, I am trying to save a SystemUser entity in JPA. I also want to save certain things like who created the SystemUser and who last modified the system User as well.
@ManyToOne(targetEntity = SystemUser.class)
@JoinColumn
private SystemUser userWhoCreated;
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(iso=ISO.DATE_TIME)
private Date timeCreated;
@ManyToOne(targetEntity = SystemUser.class)
@JoinColumn
private SystemUser userWhoLastModified;
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(iso=ISO.DATE_TIME)
private Date timeLastModified;
I also want to ensure that these values are not null when persisted. So If I use the NotNull JPA annotation, that is easily solved (along with reference to another entity)
The problem description is simple, I cannot save rootuser without having rootuser in the system if I am to use a DataLoader class to persist JPA entity. Every other later user can be easily persisted with userWhoModified as the "systemuser" , but systemuser it's self cannot be added in this scheme.
Is there a way so persist this first system user (I am thinking with SQL). This is a typical bootstrap (chicken or the egg) problem i suppose.