4

我正在使用 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
4

2 回答 2

1

尝试这个

    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);
于 2013-01-14T12:12:48.487 回答
0

尝试在 getRequest 方法上添加 {CascadeType.PERSIST, CascadeType.MERGE}

于 2013-01-14T12:19:10.690 回答