我有一个实体,请求
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().有人能解释一下我哪里做错了吗。你的帮助非常感谢。
编辑:,我只想持久化请求和RequestDevice。我已经有了RequestStatusType,RequestType,DeviceStatusType,PinType的数据。如果您需要更多的信息,请告诉我。
发布于 2014-08-14 05:04:57
检查状态
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;
..
}https://stackoverflow.com/questions/25298168
复制相似问题