[英]PersistentObjectException: detached entity passed to persist
In my app I'm connected to a websocket. 在我的应用程序中,我连接到websocket。 The socket is sending JSON data.
套接字正在发送JSON数据。 As it is becoming available I process it in a different Job.
随着它变得可用,我在不同的工作中处理它。 In each job, I convert the JSON to a model object with GSON.
在每个作业中,我使用GSON将JSON转换为模型对象。 There are 3 types of models, all independent, and with one common timestamp field in a MappedSuperclass.
有3种类型的模型,都是独立的,并且在MappedSuperclass中有一个共同的时间戳字段。 They don't have an id explicit field.
他们没有id显式字段。 I call save on the model and in 1 out of 1000 times a "PersistentObjectException: detached entity passed to persist" is thrown.
我在模型上调用save,在1000次中有1次抛出“PersistentObjectException:传递给persist的分离实体”。
The multiple jobs are called from a start up Job that connects to the websocket. 从连接到websocket的启动Job调用多个作业。 The library I'm using creates a different thread for each incoming message.
我正在使用的库为每个传入消息创建一个不同的线程。 I then convert the thread into a job before saving to the database.
然后我在保存到数据库之前将线程转换为作业。 I do this, because otherwise another PersistentObjectException is thrown about changing the id from 1 to 2 or something similar, if I allow the original thread to call save.
我这样做,因为如果我允许原始线程调用save,则会抛出另一个关于将id从1更改为2或类似内容的PersistentObjectException。
I also have another job running that accesses the database at the same time. 我还有另一个正在运行的作业,它同时访问数据库。 What could be wrong?
可能有什么不对?
@Override
public void onMessage(final WebSocketMessage message){
new Job() {
@Override
public void doJob() {
processMessage(message.getText());
}
}.now();
}
public void processMessage(String message) {
Appointment appointment = new Gson().fromJson(message, Appointment.class);
appointment.save();
}
@Entity
public class Appointment extends CalendarEvent {
private String owner;
}
@MappedSuperclass
public abstract class CalendarEvent extends Model {
private long timestamp;
}
EDIT: Added some code sample 编辑:添加了一些代码示例
Basically it means the EntityManager
won't track them any more: This is a good overview . 基本上它意味着
EntityManager
将不再跟踪它们: 这是一个很好的概述 。 How they come to be considered detached when you didn't explicitly ask for it is odd. 当你没有明确要求它们时,它们如何被认为是分离的是奇怪的。 But the workaround might be to use a "defensive" approach in your DAO method and
merge
these odd cases; 但解决方法可能是在DAO方法中使用“防御性”方法并
merge
这些奇怪的情况; ie: 即:
public void save (Model possiblyDetachedModel) {
if (entityManager.contains(possiblyDetachedModel)) {
entityManager.merge(possiblyDetachedModel);
} else {
entityManager.persist(possiblyDetachedModel);
}
}
I don't particularly like it, because it shouldn't need to be done when you're persist
ing brand-new objects. 我并不特别喜欢它,因为当你
persist
全新的物品时,不应该这样做。 It might be worth putting some logging in (or debugging if at all possible) the " merge
" branch and really inspecting those rogue objects - I'm pretty sure the EntityManager
can only use the @Id
field as the detection mechanism... 可能值得一些登录(或尽可能调试)“
merge
”分支并真正检查那些流氓对象 - 我很确定EntityManager
只能使用@Id
字段作为检测机制......
I've found this workaround to work for now. 我发现这个解决方法现在可以使用了。 Maybe it's to do with how Gson and Jpa interact.
也许这与Gson和Jpa如何互动有关。 Not sure...
不确定...
public void processMessage(String message) {
Appointment appointment = new Gson().fromJson(message, Appointment.class);
//recreate appointment
appointment = new Appointment(appointment);
appointment.save(); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.