gpt4 book ai didi

java - 使用 Morphia 检索引用的文档会返回奇怪的错误

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

我一直在使用 Morphia,因为我在使用 Spring Data for Mongodb 处理引用文档时遇到了问题。下面是我拥有的数据和代码的快照...

这是我的 User 和 Group 类(省略了 getter 和 setter)

用户类别

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import org.mongodb.morphia.annotations.Reference;



@Entity(value="users")
public class User {

@Id
private String id;
private String username;
private String firstname;
private String lastname;
private String email;
private long cellnumber;
private Date datejoined;
private boolean active;
private Account account;

@Reference(value="usergroups", lazy=true)
private List<Group> usergroups = new ArrayList<>();

}

小组课。

@Entity(value="groups")
public class Group {
@Id
private String id;
private String name;
private String description;
private long creationdate;

@Reference
private User user;

}

我现在在主方法中运行以下方法,名为 scene1() 和 scene2()。

    private void scenario1() {

User newUser = setupUser(new User());
datastore.save(newUser);
Query<User> query = datastore.createQuery(User.class)
.field("firstname").equal("Jome");
User user = query.get();

Account account = new Account();
account.setBalance(0.0);
account.setSmsvalue(0.18);

UpdateOperations<User> update = datastore
.createUpdateOperations(User.class);
update.set("account", account);

user.setAccount(account);
datastore.save(user);
List<Group> groups = query.get().getUsergroups();

Group group1 = new Group();
group1.setCreationdate(new Date().getTime());
group1.setDescription("test group for first user");
group1.setName("Group One");
group1.setUser(query.get());
datastore.save(group1);
groups.add(group1);
update.add("usergroups", group1);
datastore.update(query, update);

Group group2 = new Group();
group2.setCreationdate(new Date().getTime());
group2.setDescription("Another test group for first user");
group2.setName("Group Two");
group2.setUser(query.get());
datastore.save(group2);
groups.add(group2);

update.add("usergroups", group2);

datastore.update(query, update);

System.out.println(user);
List<Group> savedgroups = user.getUsergroups();
System.out.println(savedgroups);
}

这是输出

User [id=52e8894ef148a7f866b9f1ac, username=jomski2013, firstname=Jome, lastname=Akpoduado, email=jomea@yookos.com, cellnumber=123456789]
[Group [id=52e8894ef148a7f866b9f1ad, name=Group One, description=test group for first user, creationdate=1390971214880, owner=User [id=52e8894ef148a7f866b9f1ac, username=jomski2013, firstname=Jome, lastname=Akpoduado, email=jomea@example.com, cellnumber=123456789]], Group [id=52e8894ef148a7f866b9f1ae, name=Group Two, description=Another test group for first user, creationdate=1390971214884, owner=User [id=52e8894ef148a7f866b9f1ac, username=jomski2013, firstname=Jome, lastname=Akpoduado, email=jomea@example.com, cellnumber=123456789]]]

以及 mongo 控制台的 View

> db.users.find().pretty()
{
"_id" : ObjectId("52e8894ef148a7f866b9f1ac"),
"account" : {
"balance" : 0,
"smsvalue" : 0.18
},
"active" : true,
"cellnumber" : NumberLong("123456789"),
"className" : "org.imanmobile.sms.core.domain.User",
"datejoined" : ISODate("2014-01-29T04:53:34.746Z"),
"email" : "jomea@example.com",
"firstname" : "Jome",
"lastname" : "Akpoduado",
"password" : "$2a$10$c0SAy7eJZv06eqmAWQNUP.YrXtB7tDOdi11lkZqlAVgzVGU9RCbCS",
"usergroups" : [
DBRef("groups", "52e8894ef148a7f866b9f1ad"),
DBRef("groups", "52e8894ef148a7f866b9f1ae")
],
"username" : "jomski2013"
}

> db.groups.find().pretty()
{
"_id" : ObjectId("52e8894ef148a7f866b9f1ad"),
"className" : "org.imanmobile.sms.core.domain.Group",
"name" : "Group One",
"description" : "test group for first user",
"creationdate" : NumberLong("1390971214880"),
"user" : DBRef("users", "52e8894ef148a7f866b9f1ac")
}
{
"_id" : ObjectId("52e8894ef148a7f866b9f1ae"),
"className" : "org.imanmobile.sms.core.domain.Group",
"name" : "Group Two",
"description" : "Another test group for first user",
"creationdate" : NumberLong("1390971214884"),
"user" : DBRef("users", "52e8894ef148a7f866b9f1ac")
}

但是,当我随后运行下面的 scene2() 时

    private void scenario2() {
Query<User> query = datastore.createQuery(User.class);

User user = query.field("username").equal("jomski2013").get();
System.out.println("Number of groups: " + user.getUsergroups().size());
}

我收到错误...

Caused by: org.mongodb.morphia.mapping.MappingException: The reference({ "$ref" : "groups", "$id" : "52e8894ef148a7f866b9f1ad" }) could not be fetched for org.imanmobile.sms.core.domain.User.usergroups
at org.mongodb.morphia.mapping.ReferenceMapper.resolveObject(ReferenceMapper.java:304)
at org.mongodb.morphia.mapping.ReferenceMapper$1.eval(ReferenceMapper.java:243)
at org.mongodb.morphia.utils.IterHelper.loopOrSingle(IterHelper.java:89)
at org.mongodb.morphia.mapping.ReferenceMapper.readCollection(ReferenceMapper.java:239)
at org.mongodb.morphia.mapping.ReferenceMapper.fromDBObject(ReferenceMapper.java:163)
at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:602)
at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:584)
at org.mongodb.morphia.mapping.Mapper.fromDBObject(Mapper.java:294)
at org.mongodb.morphia.query.MorphiaIterator.convertItem(MorphiaIterator.java:71)
at org.mongodb.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:58)
at org.mongodb.morphia.query.MorphiaIterator.next(MorphiaIterator.java:53)
at org.mongodb.morphia.query.QueryImpl.get(QueryImpl.java:408)
at org.imanmobile.sms.PlayClass.scenario2(PlayClass.java:35)
at org.imanmobile.sms.PlayClass.run(PlayClass.java:29)
at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:644)

我的问题是为什么 Morphia 能够获取第一次保存的组,但随后却无法获取?有什么不同或者我应该真正注意或遗漏什么?

最佳答案

用户组 DBRef 数组与这些引用的文档之间存在类型不匹配:

"usergroups" : [
DBRef("groups", "52e82c05f148a7310a899a50"),
DBRef("groups", "52e82c05f148a7310a899a4f")
]

$ref.id 的类型为String

而 groups 集合中的 _id 类型为 ObjectId:

“_id”:ObjectId(“52e82c05f148a7310a899a4f”)

“_id”:ObjectId(“52e82c05f148a7310a899a50”)

这就是无法获取引用的原因。

您不必运行辅助查询,这是由 Morphia 自动完成的。

很难确定这种不匹配的原因。难道最初在您的 Java Group 类中您有private String ObjectId;字段,之前用于插入,后来改为private String id;

在任何情况下,要解决此问题,您都必须使用 ObjectId$ref.id 值包装在 usergroups 数组中来解决此问题:

"usergroups" : [
DBRef("groups", ObjectId("52e82c05f148a7310a899a50")),
DBRef("groups", ObjectId("52e82c05f148a7310a899a4f"))
]

您还必须相应地更改您的 Group 映射(将 id 属性设置为 ObjectId)

如果您想保持原样(将所有内容视为字符串),只需删除数据库(我猜您只是在尝试),然后从头开始

关于java - 使用 Morphia 检索引用的文档会返回奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21419364/

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