Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.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
Jpa 实体对象未持久化:通过未标记的关系找到了新对象_Jpa_Persistence_Eclipselink - Fatal编程技术网

Jpa 实体对象未持久化:通过未标记的关系找到了新对象

Jpa 实体对象未持久化:通过未标记的关系找到了新对象,jpa,persistence,eclipselink,Jpa,Persistence,Eclipselink,我正在尝试保留以下实体: public class CommentEntity { @Id @ManyToOne(optional=false,cascade=CascadeType.REFRESH, targetEntity=UserEntity.class) @JoinColumn(name="USERID",referencedColumnName="USERID") private UserEntity userEntity; @ManyToOne(optional=false,c

我正在尝试保留以下实体:

public class CommentEntity {

@Id

@ManyToOne(optional=false,cascade=CascadeType.REFRESH, targetEntity=UserEntity.class)
@JoinColumn(name="USERID",referencedColumnName="USERID")
private UserEntity userEntity;

@ManyToOne(optional=false,cascade=CascadeType.REFRESH, targetEntity=PostEntity.class)
@JoinColumn(name="POSTID",referencedColumnName="POSTID")
private PostEntity postEntity;

}
但错误是:

在同步过程中,通过未标记为cascade PERSIST:entity的关系找到了一个新对象。PostEntity@16afec.

当我尝试持久化PostEntity时,它被持久化。不会引发此类异常:

public class PostEntity {

@ManyToOne(optional = false, cascade = CascadeType.REFRESH, fetch = FetchType.LAZY, targetEntity = UserEntity.class)
@JoinColumn(name = "USERID", referencedColumnName = "USERID")
private UserEntity userEntity;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "postEntity", fetch = FetchType.LAZY)
private List<CommentEntity> commentEntityList;
}
我无法理解原因是什么?那么,为什么PostEntity会在同样的情况下持久化呢


我使用EclipseLink

是因为您试图持久化CommentEntity,其中userEntity或postEntity(或两者)引用了未持久化的实体。调用em.persist(commentEntity的实例)不会导致这些实体被持久化,因为您只级联刷新。您应该通过单独调用em.persist来持久化这些实体


如果这不能回答您的问题,请提供创建实体并将其持久化的代码。

您已编辑,但在持久化实体时,仍然没有显示实体引用的内容(如果有)。因为它的关系并没有设置为级联所有,如果它引用任何新实体(例如postEntity的新实例,它们将保持不持久。如果持久化postEntity,它会将关系级联到新的CommentEntity。请在设置关系之前持久化所有实体,或更改级联设置,以便持久化级联到所有所需的新实体。
public class UserEntity {

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "userEntity")
private List<PostEntity> postEntityList;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "userEntity")
private List<CommentEntity> commentEntityList;
}
entityManagerFactory = Persistence
                    .createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
            entityManager = entityManagerFactory.createEntityManager();
            entityTransaction = entityManager.getTransaction();
            entityTransaction.begin();
            entityManager.persist(commentEntity);
            entityTransaction.commit();
            entityManager.close();