当前位置: 首页 > 知识库问答 >
问题:

对象引用了未保存的瞬态实例-在刷新Hibernate之前保存瞬态实例

薛栋
2023-03-14

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

Request.java

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

private Long id;

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

请求事件.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

共有2个答案

丁鸿信
2023-03-14

尝试添加{CascadeType。坚持,CascadeType。getRequest方法上的MERGE}

严繁
2023-03-14

试试这个

    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);
 类似资料: