Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring JPA中的自引用对象_Spring_Jpa_Spring Roo - Fatal编程技术网

Spring JPA中的自引用对象

Spring JPA中的自引用对象,spring,jpa,spring-roo,Spring,Jpa,Spring Roo,我试图在JPA中保存一个SystemUser实体。我还想保存某些内容,比如谁创建了系统用户,以及谁上次修改了系统用户 @ManyToOne(targetEntity = SystemUser.class) @JoinColumn private SystemUser userWhoCreated; @Temporal(TemporalType.TIMESTAMP) @DateTimeFormat(iso=ISO.DATE_TIME) private Date timeCreated; @Ma

我试图在JPA中保存一个SystemUser实体。我还想保存某些内容,比如谁创建了系统用户,以及谁上次修改了系统用户

@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;
我还想确保这些值在持久化时不为null。因此,如果我使用NotNull JPA注释,这很容易解决(以及对另一个实体的引用)

问题描述很简单,如果我要使用DataLoader类来持久化JPA实体,我无法在系统中没有rootuser的情况下保存rootuser。其他所有后来的用户都可以很容易地使用userWhoModified作为“systemuser”来持久化,但systemuser自身不能添加到此方案中


有没有一种方法可以让第一个系统用户(我正在考虑使用SQL)持久化。我想这是一个典型的引导问题。

你试过
rootUser.setUserWhoLastModified(rootUser)

你试过
rootUser.setUserWhoLastModified(rootUser)

@Bozho-LOL,我第一次看到反社区维基警察:)@Bozho-LOL,我第一次看到反社区维基警察:)是的,我真的试过了,它在储存的时候起作用。在使用ROOs toString()时,我遇到了一个递归引用问题,因此rootUsers to string将有一行包含rooosers to string(在这一点上很疯狂),删除该行可以解决问题,但我仍然怀疑是否有更干净的解决方案(这意味着我必须让人们知道不要在解决方案中使用这种特殊的toString方法。我看到的另一种解决方案是使用惰性和字节码插装,到目前为止我还没有找到这个解决方案。是的,我确实尝试过,它在存储时有效。我在使用ROOs toString()时遇到了递归引用问题。)所以rootUsers-to-string将有一个行,其中包含了rooousers-to-string(它在这方面做得很疯狂),这就解决了这个问题,但我仍然想知道是否有更干净的解决方案(这意味着我必须让人们知道,在解决方案中不要使用这种特殊的toString方法。我看到的另一种解决方案是使用惰性和字节码插装,到目前为止我还没有找到这个解决方案