views:

60

answers:

1

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.

A: 

Have you tried rootUser.setUserWhoLastModified(rootUser) ?

Bozho
Yes, I actually tried it and it works when storing. I was having a recursive reference problem when using ROOs toString() so rootUsers to string will have a line which has rooUsers to string (and it went nuts on that) Removing that solves the problem, but still I wonder if there is a cleaner solution (this means that I have to let people know not to use this particular kind of toString method in the solution. Another solution which I saw was by using LAZY and bytecodeinstrumentation, I haven't figured that one out so far
geoaxis