我有一个实体,要求

class Request {
    ---------
    ---------
    //bi-directional many-to-one association to RequestStatusType
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="STATUS", nullable=false)
    private RequestStatusType requestStatusType;

    //bi-directional many-to-one association to RequestType
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="REQUEST_TYPE_ID")
    private RequestType requestType;

    //bi-directional many-to-one association to RequestDevice
    @OneToMany(mappedBy="request", cascade=CascadeType.PERSIST)
    private List<RequestDevice> requestDevices;
    --------
    --------
}


这是RequestStatusType,

class RequestStatusType{
    --------
    --------
    //bi-directional many-to-one association to Request
    @OneToMany(mappedBy="requestStatusType")
    private List<Request> requests;
    --------
    --------
}


这是RequestType,

class RequestType{
    -------
    -------
    //bi-directional many-to-one association to Request
    @OneToMany(mappedBy="requestType",cascade=CascadeType.MERGE)
    private List<Request> requests;
    -------
    -------
}


这是我的RequestDevice,

class RequestDevice{
    --------
    --------
    //bi-directional many-to-one association to DeviceStatusType
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="STATUS", nullable=false)
    private DeviceStatusType deviceStatusType;

    //bi-directional many-to-one association to PinType
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="PIN_TYPE_ID")
    private PinType pinType;

    //bi-directional many-to-one association to Request
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="REQUEST_ID")
    private Request request;

    --------
    --------
}


这是DeviceStatusType

class DeviceStatusType{
    -------
    -------
    //bi-directional many-to-one association to RequestDevice
    @OneToMany(mappedBy="deviceStatusType")
    private List<RequestDevice> requestDevices;
    -------
    -------
}


这是我的PinType

class PinType{
    -------
    -------
    //bi-directional many-to-one association to RequestDevice
    @OneToMany(mappedBy="pinType")
    private List<RequestDevice> requestDevices;
    -------
    -------
}


准备好所有实体后,当我坚持使用纯Java时,效果很好

entityManager.getTransaction().begin();
entityManager.persist(request);
entityManager.flush();
entityManager.getTransaction().commit();


但是当我像下面这样骑骆驼时

.to("jpa:com.labs.model.Request?usePersist=true&flushOnSend=true")


这给我一个错误

Encountered unmanaged object "com.labs.model.DeviceStatusType-1" in life cycle state  unmanaged while cascading persistence via field "com.labs.model.RequestDevice.deviceStatusType" during flush.  However, this field does not allow cascade persist. You cannot flush unmanaged objects or graphs that have persistent associations to unmanaged objects.
Suggested actions: a) Set the cascade attribute for this field to CascadeType.PERSIST or CascadeType.ALL (JPA annotations) or "persist" or "all" (JPA orm.xml),
 b) enable cascade-persist globally,
 c) manually persist the related field value prior to flushing.
 d) if the reference belongs to another context, allow reference to it by setting StoreContext.setAllowReferenceToSiblingContext().


有人可以解释我在哪里做错了。非常感谢您的帮助。

编辑:
我只想保留Request和RequestDevice。我已经有RequestStatusType,RequestType,DeviceStatusType,PinType的数据。如果您需要更多信息,请告诉我。

最佳答案

检查状态

persistence via field "com.labs.model.RequestDevice.deviceStatusType" during flush
.....
Set the cascade attribute for this field to CascadeType.PERSIST or CascadeType.ALL.


你的目的是什么?是否想与RequestDevice一起保留DeviceStatusType?如果是这样,则必须使用CascadeType

class RequestDevice {
    ...
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="STATUS", nullable=false)
    private DeviceStatusType deviceStatusType;
    ..
}


在代码上方,当您持久化RequestDevice时,EntityManager假定引用DeviceStatusType在日期数据库中已经存在。否则,您将得到类似错误状态的错误。
如果您要坚持在一起,请尝试以下方法。

class RequestDevice {
    ...
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) or @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.CascadeType.PERSIST)
    @JoinColumn(name = "STATUS", nullable = false)
    private DeviceStatusType deviceStatusType;
    ..
}

10-04 18:54