Ali Ahmad
Ali Ahmad

Reputation: 1055

How to Persist ArrayList within Spring Entity class?

I have the following below member variable declaration for Users entity class and have created UserRepository for the entity and have autowired the UserRepository instance in the controller class.

All was working fine, but when I declared an array-list in my entity class, my controllers crashes.

My question is how can I declare list within entity class and access it through public function in controller.

@Entity
public class Users {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String name;

    private  ArrayList <String> courses = new ArrayList<String>();

   //Omitted Class Member Functions
    }

Following is error trace

 - ERROR 3040 --- [           main]
   org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful:
   alter table video_courses drop constraint FK_gb79j8mlvu17uvv38mp0x9ts
 - 2014-08-22 11:10:07.210 ERROR 3040 --- [           main]
   org.hibernate.tool.hbm2ddl.SchemaExport  : user lacks privilege or
   object not found: PUBLIC.VIDEO_COURSES

Upvotes: 2

Views: 11595

Answers (1)

Ali Ahmad
Ali Ahmad

Reputation: 1055

I missed the @ElementCollection and @CollectionTable annotation in my entity declaration.

 @Entity
    public class Users {

            @Id
            @GeneratedValue(strategy = GenerationType.AUTO)
            private long id;

            private String name;

            @ElementCollection
            @CollectionTable(name="listOfUsers")
            private  ArrayList <String> courses = new ArrayList<String>();

           //Omitted Class Member Functions
        }

Upvotes: 6

Related Questions