gpt4 book ai didi

java - Spring Data & mongodb 转换器错误 : java. lang.StackOverflowError

转载 作者:行者123 更新时间:2023-11-29 05:33:06 25 4
gpt4 key购买 nike

我正在使用 Spring 数据连接到 mongodb。请在下面查看我的问题:

现在,我有两个数据模型(用户和草稿):

@Document

public class User implements Serializable {
@Id
private String id;
private String showName;
private String password;
//..... (other attributes)

@DBRef
private List<Draft> createdDraft = new ArrayList<Draft>();

//constructors
public User() {
super();
}

public User(String id, String showName, String password, //....other attributes
List<Draft> createdDraft) {
super();
this.id = id;
this.showName = showName;
this.password = password;
//....
}

//getters and setters
}

@Document

public class Draft {
@Id
private String id;
private String title;
private Date createTime;
private Date lastEditTime;
@DBRef
private User lastEditor;
@DBRef
private User mainAuthor;
@DBRef
private List<User> coAuthors = new ArrayList<User>();
private String externalURL;

//constructors..
public Draft() {
super();
}

public Draft(String id, String title, Date createTime, Date lastEditTime,
User lastEditor, User mainAuthor, String externalURL) {
super();
this.id = id;
this.title = title;
this.createTime = createTime;
this.lastEditTime = lastEditTime;
this.lastEditor = lastEditor;
this.mainAuthor = mainAuthor;
this.externalURL = externalURL;
}

//getters and setters...

}

在我的项目中,我已经成功创建了用户然后,我想为现有用户添加草稿。

public String CreateNewDraft(User mainAuthor)
{
Draft draft = new Draft();

draft.setMainAuthor(mainAuthor);

Date now = new Date(System.currentTimeMillis());

draft.setCreateTime(now);

mainAuthor.getCreatedDraft().add(draft);

//insert the draft --> Successful (1)

mongoOps.insert(draft);

//update the user --> Successful (2)

mongoOps.save(mainAuthor);

//find the last inserted draft. --> Errors.

Draft d = mongoOps.findOne(query(where("createTime").is(now) ), Draft.class);

return d.getId()
}

在 (1) 中,我发现了一个在 mongoDB 中创建的新“草稿”文档,它有 _id = 它有 52a1591597d738f7b397be96。

在 (2) 中,我发现现有用户 (mainAuhtor) 文档在 createdDraft 字段中添加了一个条目,例如[{“$ref”:“草稿”,“$id”:{“$oid”:“52a1591597d738f7b397be96”}}]

异常&日志:

processing failed; nested exception is java.lang.StackOverflowError

org.springframework.web.servlet.DispatcherServlet.
doDispatch(DispatcherServlet.java:972)
org.springframework.web.servlet.DispatcherServlet.
doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.p
rocessRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.d
oGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet .java:621)
javax.servlet.http.HttpServlet.service(HttpServlet .java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilt
er(WsFilter.java:51)

根本原因

java.lang.StackOverflowError 

java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.read(Unknown Source)
java.net.SocketInputStream.read(Unknown Source)
java.io.BufferedInputStream.fill(Unknown Source)
java.io.BufferedInputStream.read1(Unknown Source)
java.io.BufferedInputStream.read(Unknown Source)
org.bson.io.Bits.readFully(Bits.java:46)
org.bson.io.Bits.readFully(Bits.java:33)
org.bson.io.Bits.readFully(Bits.java:28)
com.mongodb.Response.<init>(Response.java:40)
com.mongodb.DBPort.go(DBPort.java:124)
com.mongodb.DBPort.call(DBPort.java:74)
com.mongodb.DBTCPConnector.innerCall(DBTCPConnecto r.java:286)
com.mongodb.DBTCPConnector.call(DBTCPConnector.jav a:257)
com.mongodb.DBApiLayer$MyCollection.__find(DBApiLa yer.java:310)
com.mongodb.DBApiLayer$MyCollection.__find(DBApiLa yer.java:295)
com.mongodb.DBCollection.findOne(DBCollection.java :346)
com.mongodb.DBCollection.findOne(DBCollection.java :331)
com.mongodb.DBRefBase.fetch(DBRefBase.java:53)
org.springframework.data.mongodb.core.convert.Mapp
ingMongoConverter.readValue(MappingMongoConverter. java:1046)
org.springframework.data.mongodb.core.convert.Mapp
ingMongoConverter.access$100(MappingMongoConverter .java:77)
org.springframework.data.mongodb.core.convert.Mapp
ingMongoConverter$MongoDbPropertyValueProvider.get
PropertyValue(MappingMongoConverter.java:999)
org.springframework.data.mongodb.core.convert.Mapp
ingMongoConverter.getValueInternal(MappingMongoCon verter.java:755)
org.springframework.data.mongodb.core.convert.Mapp
ingMongoConverter$2.doWithAssociation(MappingMongo Converter.java:265)
org.springframework.data.mapping.model.BasicPersis
tentEntity.doWithAssociations(BasicPersistentEntit y.java:269)
org.springframework.data.mongodb.core.convert.Mapp
ingMongoConverter.read(MappingMongoConverter.java: 262)
org.springframework.data.mongodb.core.convert.Mapp
ingMongoConverter.read(MappingMongoConverter.java: 223)
org.springframework.data.mongodb.core.convert.Mapp
ingMongoConverter.readCollectionOrArray(MappingMon
goConverter.java:788)
org.springframework.data.mongodb.core.convert.Mapp
ingMongoConverter.readValue(MappingMongoConverter. java:1048)
org.springframework.data.mongodb.core.convert.Mapp
ingMongoConverter.access$100(MappingMongoConverter .java:77)
org.springframework.data.mongodb.core.convert.Mapp
ingMongoConverter$MongoDbPropertyValueProvider.get
PropertyValue(MappingMon```

谁能帮我看看?非常感谢!

最佳答案

这是 spring data mongodb 的错误(或预期行为?)(我在 1.3.x 版本中得到它,没有尝试过 1.4.x)。

问题是 User 引用了同一个用户实例的 Draft 和 Draft,因此转换器进入无限循环。

@Document
public class User implements Serializable {
...

@DBRef
private List<Draft> createdDraft = new ArrayList<Draft>();

@Document
public class Draft {
...

@DBRef
private User lastEditor;
@DBRef
private User mainAuthor;
@DBRef
private List<User> coAuthors = new ArrayList<User>();

您可能应该使用简单的 id 引用,而不是 DBRef(这里甚至建议 http://docs.mongodb.org/manual/reference/database-references/ 适合大多数用例)

如果你发现自己经常使用 DBRef,你应该考虑使用不同类型的数据库,例如图数据库。

关于java - Spring Data & mongodb 转换器错误 : java. lang.StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20437405/

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