Java 如何在 Google App Engine 的数据存储中将列表添加为实体的属性?(“基本”属性类型不应是容器)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21059451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:16:34  来源:igfitidea点击:

How to add List as property of Entity in Datastore of Google App Engine? ('Basic' attribute type should not be a container)

javagoogle-app-enginejpaintellij-ideagoogle-cloud-datastore

提问by Hesam

I'm working on my first application of GAE.

我正在处理我的第一个 GAE 应用程序。

I have a class "Place" with some properties which holds some information about a place such as name of place, little bit description about it and some images URLs. Therefore I declared and list in the class with generic of Link. However, I am getting "Basic attribute type should not be a container". I tried to remove @Basicbut the error message is still there.

我有一个“Place”类,其中包含一些属性,其中包含一些关于地点的信息,例如地点名称、关于它的一些描述和一些图像 URL。因此,我使用Link 的泛型在类中声明并列出。但是,我得到了“ Basic attribute type should not be a container”。我试图删除@Basic但错误消息仍然存在。

I remember in MySQL I should design another table for image and based on placeId link place and images together. should I follow this way?

我记得在 MySQL 中,我应该为图像设计另一个表,并基于 placeId 将位置和图像链接在一起。我应该这样吗?

Based on what I found from "Types and Property Classes", it is possible to add List as attribute but I have no idea why it shows the error message.

根据我从“类型和属性类”中发现的内容,可以将 List 添加为属性,但我不知道为什么它会显示错误消息。

enter image description here

在此处输入图片说明

===============

================

Update: I designed my class based on what Dan Sandersonmentioned in his book Programming Google App Engine, page 300

更新:我根据Dan Sanderson他的书Programming Google App Engine,第 300 页中提到的内容设计了我的课程

enter image description here

在此处输入图片说明

采纳答案by Hesam

Okay the error seems not have impact on GAE since I can run the app and store data into storage. I guess it's a bug in Intellij Idea that you can simply ignore it.

好的,错误似乎对 GAE 没有影响,因为我可以运行应用程序并将数据存储到存储中。我想这是 Intellij Idea 中的一个错误,您可以简单地忽略它。

enter image description here

在此处输入图片说明

回答by Robin Green

As the message says, @Basicshould not be used for containers (e.g. Java collections). It is only to be used for a limited list of basic types. Remove the @Basicannotation on that field.

正如消息所说,@Basic不应用于容器(例如 Java 集合)。它仅用于有限的基本类型列表。删除该@Basic字段上的注释。

If, as you say in the question, the error message is still there, you might need to try the following steps in order:

如果如您在问题中所说,错误消息仍然存在,您可能需要按顺序尝试以下步骤:

  1. Save the file
  2. Close and reopen the file
  3. Clean and rebuild the project
  4. Restart the IDE
  1. 保存文件
  2. 关闭并重新打开文件
  3. 清理并重建项目
  4. 重新启动IDE

(these are generic steps, which I use when an IDE is generating a compilation error that obviously makes no sense.)

(这些是通用步骤,当 IDE 生成显然没有意义的编译错误时,我会使用这些步骤。)

回答by mikebertiean

Change @basicto @OneToManyfor List types

更改@basic@OneToMany了列表类型

回答by Amalgovinus

This can also happen when your class is missing its @Entity annotation. When you get weird warnings like these, sometimes it helps to try and compile and see if the compiler complains.

当您的类缺少其 @Entity 注释时,也会发生这种情况。当您收到这些奇怪的警告时,有时尝试编译并查看编译器是否会抱怨会有所帮助。

回答by Giorgi Tsiklauri

I know, that's an old question, but it's still relevant as I just had this problem.

我知道,这是一个老问题,但它仍然相关,因为我刚刚遇到了这个问题。

You are most likely missing relation (like @OneToMany) annotation and/or @Entityannotation.

您很可能缺少关系(如@OneToMany)注释和/或@Entity注释。

I had a same problem in

我有同样的问题

@Entity
public class SomeFee {
    @Id
    private Long id;
    private String code;
    private String name;
    private List<AdditionalFee> additionalFees;
    //getters, setters..
}

class AdditionalFee {
    @Id
    private int id;
    //getters, setters..
}

additionalFeesfield was having a problem.. and what I was missing and what helped me are the following:

additionalFees领域有问题..我所缺少的以及对我有帮助的是以下内容:

  1. @Entity- on the Generic Type argument (AdditionalFee) class;
  2. @OneToMany(or any other type of appropriate relation fitting your case) annotation on the private List<AdditionalFee> additionalFees;field.
  1. @Entity- 在泛型类型参数 ( AdditionalFee) 类上;
  2. @OneToMany(或适合您的情况的任何其他类型的适当关系)在private List<AdditionalFee> additionalFees;字段上的注释。


So, working version looked like this:


因此,工作版本如下所示:

@Entity
    public class SomeFee {
        @Id
        private Long id;
        private String code;
        private String name;
        @OneToMany
        private List<AdditionalFee> additionalFees;
        //getters, setters..
    }
    @Entity
    class AdditionalFee {
        @Id
        private int id;
        //getters, setters..
    }

回答by Fred Campos

You can also use @ElementCollection:

您还可以使用@ElementCollection

@ElementCollection
private List<String> tags;