gpt4 book ai didi

hibernate - 对象引用未保存的 transient 实例 - 在刷新 Hibernate 之前保存 transient 实例

转载 作者:行者123 更新时间:2023-12-04 08:56:27 28 4
gpt4 key购买 nike

我正在使用 Seam 框架。我有 2 个实体:

请求.java

@Entity
@Table(name = "SRV_REQUEST")
public class Request {

private Long id;

@Id
@Column(name = "ID_")
public Long getId() {
return id;
}

RequestEvent.java
@Entity
@Table(name = "SRV_REQUEST_EVENT")
public class RequestEvent {

private Long id;
private Request request;

@Id
@GeneratedValue
@Column(name = "ID_")
public Long getId() {
return id;
}

@ManyToOne
public Request getRequest() {
return request;
}

当我做这个交易时:
Request request = new Request();
// I have to set ID manually
request.setId(12345);

entityManager.persist(request);

RequestEvent event = new RequestEvent();
event.setRequest(request);

entityManager.persist(event);

我收到此错误:
object references an unsaved transient instance - save the transient instance before flushing: kz.gov.mci.ipsc.services.RequestEvent.request -> kz.gov.mci.ipsc.services.Request

最佳答案

尝试这个

    Request request = new Request();
// I have to set ID manually
request.setId(12345);

entityManager.persist(request);

entityManager.flush();//this point is important

RequestEvent event = new RequestEvent();
event.setRequest(request);

entityManager.persist(event);

关于hibernate - 对象引用未保存的 transient 实例 - 在刷新 Hibernate 之前保存 transient 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14318125/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com