英文:
Unable to Save Objectify entity with Map<string,Object> in Embedded classes?
问题
我有一个嵌入式实体,其中有一个类型为Map<String, Object>的字段。
地图的所有键都是字符串类型。
以下是实体:
@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;
}
我在保存带有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"
}]
}
}]
}
地图的所有键都是字符串类型。
错误消息:
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"
Stack Trace链接:
https://drive.google.com/file/d/1fqpPLiJutWLif5GnrlqLEZ6Wr_PdLdC-/view?usp=sharing
英文:
I have an embedded entity that has a Field of type Map<String, Object>.
All keys to the Map Are of type String.
Below is Entity
@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.
Sample Entity submitted From FrontEnd
{
"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.
Error Message:
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 to Stack Trace
https://drive.google.com/file/d/1fqpPLiJutWLif5GnrlqLEZ6Wr_PdLdC-/view?usp=sharing
答案1
得分: 1
要查看 @Stringify 的工作原理,请参阅 https://github.com/objectify/objectify/wiki/Entities#stringify:
为了在 Maps 中使用非字符串键,您可以指定 @Stringify 注解:
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<>();
}
英文:
To see how @Stringify works, look at 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<>();
}
Previous response :
The error message is clear : you must provide a "String-way" to handle your embedded entity. So you should write a toString()
method inside your embedded entity or use the @Stringify
annotation as appropriate.
It seems to be JSON, so did you try to use @JsonUnwrapped :
@see https://stackoverflow.com/a/10077262/390462
class Item {
private String title;
@JsonProperty("date")
private Date createdAt;
// How to map this?
@JsonUnwrapped
private Author author;
}
You can go through a good JPA tutorial to handle Embedded objects too :
答案2
得分: 0
使用@Serialize
注解来处理复杂对象图。
来自Objectify文档
这解决了我的问题。
@Getter @Setter
public class SurveyQuestions implements Serializable{
private static final long serialVersionUID = -6930992373082651231L;
@Serialize
private Map<String,Object> optionGroup;
}
英文:
> Use @Serialize
annotation for complex object graph.
From Objectify Docs
This Solved my issue.
@Getter @Setter
public class SurveyQuestions implements Serializable{
private static final long serialVersionUID = -6930992373082651231L;
@Serialize
private Map<String,Object> optionGroup;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论