简体   繁体   中英

Cannot declare List property in the JPA @Entity class. It says 'Basic' attribute type should not be a container

I have a JPA @Entity class Place , with some properties holding some information about a place, such as name of place, description, and URLs of some images.

For the URLs of images, I declare a List<Link> in my entity.

在此处输入图片说明

However, I am getting this error:

Basic attribute type should not be a container.

I tried to remove @Basic , but the error message is still there. I have no idea why it shows this error.

Any help?

You are most likely missing a relational (like @OneToMany ) annotation and/or @Entity annotation.

I had a same problem in:

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

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

additionalFees was the field causing the problem.

What I was missing and what helped me are the following:

  1. @Entity annotation 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.

So, the working version looked like this:

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

You can also use @ElementCollection :

@ElementCollection
private List<String> tags;

将列表类型的@basic更改为@OneToMany

As the message says, @Basic should not be used for containers (eg Java collections). It is only to be used for a limited list of basic types. Remove the @Basic annotation on that field.

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

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

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 and you can simply ignore it.

在此处输入图片说明

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.

Or you can mark it as @Transient if it doesn't exist on DB table.

@Transient
private List<String> authorities = new ArrayList<>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM