Home > front end >  Spring JPA ArrayList and Lob
Spring JPA ArrayList and Lob

Time:10-11

I'm trying to save a simple object to a JPA repository and there is something I cannot understand.

When I try to store a list of Integers like this, I get a runtime error that I cannot cast an ArrayList to a blob:

@Lob
List<Integer> bla = new ArrayList<>();

However if I do it like this:

@Lob
BogusClass bogus;

where

class BogusClass implements Serializable {
  List<Integer> bla = new ArrayList<>();
}

everything works fine and dandy. But Integers are serializable by default and in the Lob documentation it says that collections should be accepted. Can someone explain the difference to me and why the first option does not work?

CodePudding user response:

In the case of a relational database, it is not possible to store multiple values in a single column.

When you need to save the list of objects in DB, mainly you have 2 options:

  • use @ElementCollection, and @CollectionTable annotations. A new table will be created(one to many/many to one);
  • create a converter that will convert the data list into comma-separated value
  • Related