48

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.

enter image description here

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. Why does it shows this error?

1
  • 3
    Any basic JPA(2) docs will say that a List of Strings requires an ElementCollection annotation. As for why you refer to Python docs when using Java ... Jan 13, 2014 at 18:55

8 Answers 8

45

You can also use @ElementCollection:

@ElementCollection
private List<String> tags;
2
  • 4
    I used it but tags are not stored into DB
    – Mr Special
    Aug 11, 2021 at 4:02
  • Maybe you need to declare a @Column(columnDefinition = "String[]"). In my case, I have to implement that way in PostgreSQL
    – mindOf_L
    Dec 14, 2022 at 15:10
34

You are most likely missing an association mapping (like @OneToMany) and/or @Entity annotation(s).

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..
}

and 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 association that fits particular business 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..
}
9

Change @basic to @OneToMany for List types

7

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

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

As the message says, @Basic should not be used for containers (e.g. 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.)

1
  • Thanks Robin, but unfortunately didn't work :( I'm trying to figuring out what's the problem. Error message will be gone by removing "@Entity(name = "Book")", however, I don't think it's the correct way.
    – Hesam
    Jan 12, 2014 at 8:45
1

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.

1

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.

enter image description here

2
  • 3
    It is not so a bug - if an entity holds a list of another entities, this means "one to many" relation, not "basic" field. If you add @OneToMany annotation, error in IDE will disappear.
    – cybersoft
    Oct 19, 2015 at 22:23
  • @Hesam I also think that this answer is really wrong.. and IDE bug has nothing to do with this question. Apr 1, 2021 at 11:03
0

'Basic' attribute type should not be a container

This error occurs when you declare an existing entity as an attribute in the current Entity without declaring the relationship type which could be either of the JPA relationships. Detailed Article on JPA relationships

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.