Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 492
Hesam Avatar asked Jan 11 '14 06:01

Hesam


People also ask

What is the purpose of @entity in JPA?

An entity can aggregate objects together and effectively persist data and related objects using the transactional, security, and concurrency services of a JPA persistence provider.

What is the use of @entity annotation in Java?

The @Entity annotation specifies that the class is an entity and is mapped to a database table. The @Table annotation specifies the name of the database table to be used for mapping.

How do you define an entity in JPA?

Entities in JPA are nothing but POJOs representing data that can be persisted to the database. An entity represents a table stored in a database. Every instance of an entity represents a row in the table.


2 Answers

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.. } 
like image 39
Giorgi Tsiklauri Avatar answered Sep 19 '22 12:09

Giorgi Tsiklauri


You can also use @ElementCollection:

@ElementCollection private List<String> tags; 
like image 157
Fred Campos Avatar answered Sep 22 '22 12:09

Fred Campos