无法在嵌入式 类 中使用 Map<string,Object> 保存 Objectify 实体?
Unable to Save Objectify entity with Map<string,Object> in Embedded classes?
我有一个嵌入式实体,它的字段类型为 Map。
映射的所有键都是字符串类型。
下面是实体
@Entity
@Index
@Getter @Setter
public class NiftySurveys {
@Id
private Long id;
private List<SurveyQuestions> questions;
private Long createddate;
private Long updatedDate;
}
@Getter @Setter
public class SurveyQuestions {
private String label;
private String code;
private SurveyQuestionGroups questionGroup;
private Map<String,Object> optionGroup;
}
I am having trouble saving Entity with optionGroup.
从前端提交的示例实体
{
"questions": [{
"label": "jio",
"code": null,
"questionGroup": {
"name": "Date Time",
"value": "DATI"
},
"optionGroup": {
"labels": [{
"label": "Date / Time"
}],
"collectDateInfo": true,
"collectTimeInfo": true,
"dateFormat": "MM/DD/YYYY",
"validationMessage": "Please Enter a Valid Date!"
}
}, {
"code": null,
"label": "Q2",
"questionGroup": {
"name": "Multiple Choice Questions",
"value": "MCQ"
},
"optionGroup": {
"name": "Agree - Disagree",
"code": "AGDAG",
"options": [{
"label": "YES",
"value": "Y"
}, {
"label": "NO",
"value": "N"
}]
}
}]
}
All Keys of the map Are Strings.
错误信息:
exception: "com.googlecode.objectify.SaveException"
message: "Error saving com.nifty.niftyfeedbacks.domain.NiftySurveys@4ac88d5e: java.lang.IllegalStateException: Embedded Map keys must be of type String/Enum/Key<?> or field must specify @Stringify"
link 到堆栈跟踪
https://drive.google.com/file/d/1fqpPLiJutWLif5GnrlqLEZ6Wr_PdLdC-/view?usp=sharing
要了解@Stringify 的工作原理,请查看 https://github.com/objectify/objectify/wiki/Entities#stringify :
In order to use non-String keys with Maps, you may specify the @Stringify annotation:
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Stringify;
import com.googlecode.objectify.stringifier.Stringifier;
class DateStringifier implements Stringifier<LocalDate> {
@Override
public String toString(LocalDate obj) {
return obj.getString();
}
@Override
public LocalDate fromString(String str) {
return new LocalDate(str);
}
}
@Entity
class Car {
@Id Long id;
@Stringify(DateStringifier.class)
Map<LocalDate, ServiceRecord> serviceHistory = new HashMap<>();
}
之前的回复:
错误消息很明确:您必须提供“String-way”来处理您的嵌入式实体。因此,您应该在嵌入式实体中编写一个 toString()
方法,或者根据需要使用 @Stringify
注释。
好像是JSON,所以你尝试使用@JsonUnwrapped:
@see
class Item {
private String title;
@JsonProperty("date")
private Date createdAt;
// How to map this?
@JsonUnwrapped
private Author author;
}
您也可以通过一个很好的 JPA 教程来处理嵌入式对象:
Use @Serialize
annotation for complex object graph.
From Objectify Docs
这解决了我的问题。
@Getter @Setter
public class SurveyQuestions implements Serializable{
private static final long serialVersionUID = -6930992373082651231L;
@Serialize
private Map<String,Object> optionGroup;
}
我有一个嵌入式实体,它的字段类型为 Map
映射的所有键都是字符串类型。
下面是实体
@Entity
@Index
@Getter @Setter
public class NiftySurveys {
@Id
private Long id;
private List<SurveyQuestions> questions;
private Long createddate;
private Long updatedDate;
}
@Getter @Setter
public class SurveyQuestions {
private String label;
private String code;
private SurveyQuestionGroups questionGroup;
private Map<String,Object> optionGroup;
}
I am having trouble saving Entity with optionGroup.
从前端提交的示例实体
{
"questions": [{
"label": "jio",
"code": null,
"questionGroup": {
"name": "Date Time",
"value": "DATI"
},
"optionGroup": {
"labels": [{
"label": "Date / Time"
}],
"collectDateInfo": true,
"collectTimeInfo": true,
"dateFormat": "MM/DD/YYYY",
"validationMessage": "Please Enter a Valid Date!"
}
}, {
"code": null,
"label": "Q2",
"questionGroup": {
"name": "Multiple Choice Questions",
"value": "MCQ"
},
"optionGroup": {
"name": "Agree - Disagree",
"code": "AGDAG",
"options": [{
"label": "YES",
"value": "Y"
}, {
"label": "NO",
"value": "N"
}]
}
}]
}
All Keys of the map Are Strings.
错误信息:
exception: "com.googlecode.objectify.SaveException"
message: "Error saving com.nifty.niftyfeedbacks.domain.NiftySurveys@4ac88d5e: java.lang.IllegalStateException: Embedded Map keys must be of type String/Enum/Key<?> or field must specify @Stringify"
link 到堆栈跟踪 https://drive.google.com/file/d/1fqpPLiJutWLif5GnrlqLEZ6Wr_PdLdC-/view?usp=sharing
要了解@Stringify 的工作原理,请查看 https://github.com/objectify/objectify/wiki/Entities#stringify :
In order to use non-String keys with Maps, you may specify the @Stringify annotation:
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Stringify;
import com.googlecode.objectify.stringifier.Stringifier;
class DateStringifier implements Stringifier<LocalDate> {
@Override
public String toString(LocalDate obj) {
return obj.getString();
}
@Override
public LocalDate fromString(String str) {
return new LocalDate(str);
}
}
@Entity
class Car {
@Id Long id;
@Stringify(DateStringifier.class)
Map<LocalDate, ServiceRecord> serviceHistory = new HashMap<>();
}
之前的回复:
错误消息很明确:您必须提供“String-way”来处理您的嵌入式实体。因此,您应该在嵌入式实体中编写一个 toString()
方法,或者根据需要使用 @Stringify
注释。
好像是JSON,所以你尝试使用@JsonUnwrapped: @see
class Item {
private String title;
@JsonProperty("date")
private Date createdAt;
// How to map this?
@JsonUnwrapped
private Author author;
}
您也可以通过一个很好的 JPA 教程来处理嵌入式对象:
Use
@Serialize
annotation for complex object graph. From Objectify Docs
这解决了我的问题。
@Getter @Setter
public class SurveyQuestions implements Serializable{
private static final long serialVersionUID = -6930992373082651231L;
@Serialize
private Map<String,Object> optionGroup;
}