gpt4 book ai didi

java - 使用 JPA 持久化实体时面临的问题

转载 作者:太空宇宙 更新时间:2023-11-04 06:36:34 25 4
gpt4 key购买 nike

我有一个实体,请求

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();

但是当我用 Camel 做的时候,如下所示

.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.

你的目的是什么?想要将 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;
..
}

关于java - 使用 JPA 持久化实体时面临的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25298168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com