无法保存带有Map的Objectify实体在嵌入类中?

huangapple go评论66阅读模式
英文:

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&lt;SurveyQuestions&gt; questions;
	private Long createddate;
	private Long updatedDate;
}

@Getter @Setter
public class SurveyQuestions {
	private String label;
	private String code;
	private SurveyQuestionGroups questionGroup;
	private Map&lt;String,Object&gt; 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&lt;?&gt; 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&lt;SurveyQuestions&gt; questions;
	private Long createddate;
	private Long updatedDate;
}

@Getter @Setter
public class SurveyQuestions {
	private String label;
	private String code;
	private SurveyQuestionGroups questionGroup;
	private Map&lt;String,Object&gt; optionGroup;
}

>I am having trouble saving Entity with optionGroup.

Sample Entity submitted From FrontEnd

{
	&quot;questions&quot;: [{
		&quot;label&quot;: &quot;jio&quot;,
		&quot;code&quot;: null,
		&quot;questionGroup&quot;: {
			&quot;name&quot;: &quot;Date Time&quot;,
			&quot;value&quot;: &quot;DATI&quot;
		},
		&quot;optionGroup&quot;: {
			&quot;labels&quot;: [{
				&quot;label&quot;: &quot;Date / Time&quot;
			}],
			&quot;collectDateInfo&quot;: true,
			&quot;collectTimeInfo&quot;: true,
			&quot;dateFormat&quot;: &quot;MM/DD/YYYY&quot;,
			&quot;validationMessage&quot;: &quot;Please Enter a Valid Date!&quot;
		}
	}, {
		&quot;code&quot;: null,
		&quot;label&quot;: &quot;Q2&quot;,
		&quot;questionGroup&quot;: {
			&quot;name&quot;: &quot;Multiple Choice Questions&quot;,
			&quot;value&quot;: &quot;MCQ&quot;
		},
		&quot;optionGroup&quot;: {
			&quot;name&quot;: &quot;Agree - Disagree&quot;,
			&quot;code&quot;: &quot;AGDAG&quot;,
			&quot;options&quot;: [{
				&quot;label&quot;: &quot;YES&quot;,
				&quot;value&quot;: &quot;Y&quot;
			}, {
				&quot;label&quot;: &quot;NO&quot;,
				&quot;value&quot;: &quot;N&quot;
			}]
		}
	}]
}

> All Keys of the map Are Strings.

Error Message:

exception: &quot;com.googlecode.objectify.SaveException&quot;
message: &quot;Error saving com.nifty.niftyfeedbacks.domain.NiftySurveys@4ac88d5e: java.lang.IllegalStateException: Embedded Map keys must be of type String/Enum/Key&lt;?&gt; or field must specify @Stringify&quot;

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&lt;LocalDate&gt; {
    @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&lt;LocalDate, ServiceRecord&gt; serviceHistory = new HashMap&lt;&gt;();
}

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(&quot;date&quot;)
    private Date createdAt;

    // How to map this?
    @JsonUnwrapped
    private Author author;
}

You can go through a good JPA tutorial to handle Embedded objects too :

  1. https://examples.javacodegeeks.com/enterprise-java/jpa/jpa-embedded-embeddable-example/
  2. https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/embeddable-classes.html
  3. https://www.tutorialspoint.com/ejb/ejb_embeddable_objects.htm

答案2

得分: 0

使用@Serialize注解来处理复杂对象图。
来自Objectify文档

这解决了我的问题。

@Getter @Setter
public class SurveyQuestions implements Serializable{
	private static final long serialVersionUID = -6930992373082651231L;
	@Serialize
	private Map&lt;String,Object&gt; 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&lt;String,Object&gt; optionGroup;
	
	
}

huangapple
  • 本文由 发表于 2020年8月10日 17:54:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63337944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定