gpt4 book ai didi

尝试创建新对象并将其添加到 Arraylist 时出现 Java JPA LazyInitializationException

转载 作者:行者123 更新时间:2023-12-01 21:42:23 27 4
gpt4 key购买 nike

我有一个 Java spring JPA 应用程序,它有一个 Bord 实体和一个 BordRow 实体。一个 Bord 具有多个 BordRow。现在我尝试创建一个新的 BordRow 并将其添加到 Bord 中的 BordRows 列表中。但是,我收到 LazyInitializationException。我必须如何更改代码才能创建新的 BordRow、将其保存在数据库中并返回整个 Bord 对象?参数content类似于:{title: "Test Row", bord_id: 1}

异常(exception):

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: Bord.rows, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:606) ~[hibernate-core-5.4.6.Final.jar:5.4.6.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:218) ~[hibernate-core-5.4.6.Final.jar:5.4.6.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:585) ~[hibernate-core-5.4.6.Final.jar:5.4.6.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.write(AbstractPersistentCollection.java:409) ~[hibernate-core-5.4.6.Final.jar:5.4.6.Final]
at org.hibernate.collection.internal.PersistentBag.add(PersistentBag.java:383) ~[hibernate-core-5.4.6.Final.jar:5.4.6.Final]
at WebSocketController.create(WebSocketController.java:47) ~[main/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_221]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_221]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_221]
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:171) ~[spring-messaging-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:120) ~[spring-messaging-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMatch(AbstractMethodMessageHandler.java:565) [spring-messaging-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:511) [spring-messaging-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:94) [spring-messaging-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessageInternal(AbstractMethodMessageHandler.java:520) [spring-messaging-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessage(AbstractMethodMessageHandler.java:454) [spring-messaging-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at org.springframework.messaging.support.ExecutorSubscribableChannel$SendTask.run(ExecutorSubscribableChannel.java:144) [spring-messaging-5.2.0.RELEASE.jar:5.2.0.RELEASE]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_221]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_221]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_221]

边界类别:

@Table(name = "bords")
@Entity
public class Bord
{
@Id
@GeneratedValue
private int id;

@NotNull
private String name;

@Column
private String icon;

@Column
private String background;

@OneToMany(mappedBy = "bord", fetch = FetchType.LAZY)
@JsonIgnore
private List<BordRow> rows;

public Bord()
{
rows = new ArrayList<>();
}

public void addRow(BordRow row)
{
this.rows.add(row);
}

// Getters and setters
}

BordRow 类:

@Entity
@Table(name = "bord_rows")
public class BordRow
{
@Id
@GeneratedValue
private int id;

@NotNull
private String title;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "bord_id")
@JsonIgnore
private Bord bord;

public BordRow(String title) {
this.title = title;
}

// Getters and setters
}

WebsocketController 方法:

@MessageMapping("/bord_row/create")
@SendTo("/bord_row/created")
public Optional<Bord> create(String content) {
JSONObject object = new JSONObject(content);
Optional<Bord> bord = bordRepository.findById(object.getInt("bord_id"));

if(bord.isPresent())
{
BordRow row = new BordRow(object.getString("title"));
row.setBord(bord.get());
List<BordRow> rows = bord.get().getRows();
rows.add(row);
bord.get().setRows(rows);
bordRepository.save(bord.get());
}

return bord;
}

WebsocketController方法其他尝试:

@MessageMapping("/bord_row/create")
@SendTo("/bord_row/created")
public Optional<Bord> create(String content) {
JSONObject object = new JSONObject(content);
Optional<Bord> bord = bordRepository.findById(object.getInt("bord_id"));

if(bord.isPresent())
{
BordRow row = new BordRow(object.getString("title"));
row.setBord(bord.get());
bord.get().addRow(row);
bordRepository.save(bord.get());
}

return bord;
}

最佳答案

这里从数据库加载一个对象

可选 bord = bordRepository.findById(object.getInt("bord_id"))

带有 BoardRows 的列表是延迟加载的,这意味着一旦事务关闭,您将在代理上工作。这是您遇到的错误,因为您想在代理上添加一些内容。

您可以将延迟加载更改为急切加载,但由于性能原因,不建议这样做。您可以使用 EntityGraph。这些允许您加载更深的对象,即使它们是延迟加载的。

我希望这有帮助

马蒂亚斯

关于尝试创建新对象并将其添加到 Arraylist 时出现 Java JPA LazyInitializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58787498/

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