self referencing object in JPA

Posted by geoaxis on Stack Overflow See other posts from Stack Overflow or by geoaxis
Published on 2010-05-25T15:39:18Z Indexed on 2010/05/25 15:41 UTC
Read the original article Hit count: 319

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about spring

Related posts about jpa