gpt4 book ai didi

mongodb - spring data和mongodb中级联保存的实现

转载 作者:可可西里 更新时间:2023-11-01 10:42:24 24 4
gpt4 key购买 nike

我正在实现一个基于 spring 数据和 mongodb 的应用程序。

QATemplate.java

@Document(collection = "QATemplate")
public class QATemplate {

@Id
private String titleId;

public String getTitleId() {
return titleId;
}

public void setTitleId(String titleId) {
this.titleId = titleId;
}

@Field("title")
private String title;

@Field("tags")
private Object tags;

@Field("answer_type")
private String answerType;

@Field("answer")
@DBRef
@CascadeSave
private Object answer;

我有一个字段,可以是简单的文本,也可以是嵌入的文档。所以我将该字段创建为对象类型。

    @Field("answer")
private Object answer;

它运行良好。但我需要用自己的 ID 保存嵌入式文档,并将其 ID 作为引用传递到该文档中,所以我必须像这样编写代码

    @Field("answer")
@DBRef
@CascadeSave
private Object answer;

为了实现我使用的级联保存

CasecadeSave.java

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface CascadeSave {

}

CasecadeCallback.java

public class CascadeCallback implements ReflectionUtils.FieldCallback {

private Object source;
private MongoOperations mongoOperations;

public CascadeCallback(final Object source, final MongoOperations mongoOperations) {
this.source = source;
this.setMongoOperations(mongoOperations);
}

@Override
public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException {
ReflectionUtils.makeAccessible(field);

if (field.isAnnotationPresent(DBRef.class) && field.isAnnotationPresent(CascadeSave.class)) {
final Object fieldValue = field.get(getSource());

boolean insta = !(fieldValue instanceof String);
boolean instanull = fieldValue != null;

if ( instanull && insta ) {
final FieldCallback callback = new FieldCallback();
ReflectionUtils.doWithFields(fieldValue.getClass(), callback);
getMongoOperations().save(fieldValue);
}
}
}

public Object getSource() {
return source;
}

public void setSource(final Object source) {
this.source = source;
}

public MongoOperations getMongoOperations() {
return mongoOperations;
}

public void setMongoOperations(final MongoOperations mongoOperations) {
this.mongoOperations = mongoOperations;
}
}

CascadeSaveMongoEventListener.java

public class CascadeSaveMongoEventListener extends AbstractMongoEventListener<Object> {

@Autowired
private MongoOperations mongoOperations;

@Override
public void onBeforeConvert(final Object source) {
ReflectionUtils.doWithFields(source.getClass(), new CascadeCallback(source, mongoOperations));
}
}

FieldCallback.java

public class FieldCallback implements ReflectionUtils.FieldCallback {
private boolean idFound;

@Override
public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException {
ReflectionUtils.makeAccessible(field);

if (field.isAnnotationPresent(Id.class)) {
idFound = true;
}
}

public boolean isIdFound() {
return idFound;
}
}

但在应用此代码后,如果我像这样插入 json 数据

{
"title":"Indiaa",
"answerType":"text",
"answer":[
{
"title":"Indiaana jones",
"answerType":"text",
"answer":"testing vvv"
}
]
}

我遇到了一个错误:

threw exception [Request processing failed; 
nested exception is java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to com.mongodb.BasicDBList] with root cause
java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to com.mongodb.BasicDBList

如果我尝试像这样插入 json 数据:

{
"title":"Indiava",
"answerType":"text",
"answer":"vv"
}

我遇到了一个错误:

HTTP Status 500 - Request processing failed; 
nested exception is org.springframework.data.mapping.model.MappingException: No mapping metadata found for class java.lang.String

最佳答案

当使用 DBRef 时,您不会在 QATemplate 中保存您的 answer 对象,您只是保存了对它的引用,但它是在另一个集合中:

{ "$ref" : "COLLECTION", "$id" : ObjectId("someobjectID") }

此外,你说你想要一个答案“文档”,但实际上你首先传递了一个数组,然后传递了一个简单的字符串。

要使其运行,请创建一个包含答案文档和字符串答案的类型。这样 Spring Data MongoDB 就会知道将它放在哪个集合中。首先尝试只使用一个文档,如下所示:

{
"title":"Where is India",
"answerType":"text",
"answer":{
"text":"Asia"
}
}

answer 是:

@Field("answer")
@DBRef
@CascadeSave
private Answer answer;

答案是:

@Document(collection = "Answers")
public class Answer {
@Id
private String id;

String text;
}

一旦运行,将 Answer 变成一个界面,然后有一个 TextAnswer(简单文本)或 FullAnswer(一个完整的答案文档) . Spring Data MongoDB 支持多态性。

关于mongodb - spring data和mongodb中级联保存的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36195245/

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