面对使用JPA持久化实体的问题

时间:2014-08-14 00:12:06

标签: java jpa apache-camel

我有一个实体,请求

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的数据。如果您需要更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:1)

检查状态

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

你的目的是什么?希望将RequestDeviceDeviceStatusType一起保留?如果是这样,您必须使用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;
    ..
}