从谷歌应用引擎中的实体获取Text属性

时间:2012-05-12 17:50:28

标签: google-app-engine google-cloud-datastore

我使用entityName.setProperty("textProperty", text)将一些大型文本数据作为Text对象存储到Google App引擎数据存储区中 其中“text”是文本类型对象。当我尝试获取文本时,我使用entityName.getProperty("textProperty"); 因为它返回一个对象并将其存储为Text对象,所以我只使用(Text)entityName.getProperty("textProperty");将其转换为Text 但它给了我java.lang.ClassCastException: java.lang.String cannot be cast to com.google.appengine.api.datastore.Text 所以看起来应用程序将它存储为String,所以我做(String)entityName.getProperty("textProperty"); 但它给了我这个 java.lang.ClassCastException: com.google.appengine.api.datastore.Text cannot be cast to java.lang.String

我感到困惑:如何从数据存储中获取Text对象?

2 个答案:

答案 0 :(得分:1)

我使用Text text = new Text(String);从字符串转换为文本,使用String s = text.getValue();从文本转换为字符串。

答案 1 :(得分:1)

在Google APP Engine中将TextProperty与JPA映射时,我使用以下代码解决了我的问题。

我定义了一个Text属性内容,每当我做一个Set:我将传递Text。每当我执行Get时,我将使用getValue()方法返回一个String。它对我有用。

@Column(name="content")
private Text content;
public void setContent(Text content) {
    this.content = content;
}
public String getContent() {
    return content.getValue();
}
相关问题