Alberto Crespo Alberto Crespo - 1 year ago 61
Java Question

How save a 2D arrays (List<List<String>> list) in JPA

I am using Spring server and JPA to save the variables.

I am trying to save:

List<List<String>> Listarespostas = new ArrayList<List<String>>();

But obviously an error appears:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class palmaslab.mapas.config.Application: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(
at org.springframework.boot.SpringApplication.refresh(
at palmaslab.mapas.config.Application.main(
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(
... 15 more
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: Project_Listarespostas, for columns: [org.hibernate.mapping.Column(Listarespostas)]
at org.hibernate.mapping.SimpleValue.getType(
at org.hibernate.mapping.SimpleValue.isValid(
at org.hibernate.mapping.Collection.validate(
at org.hibernate.cfg.Configuration.validate(
at org.hibernate.cfg.Configuration.buildSessionFactory(
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(

Anyone knows an alternative?

Answer Source

To cut to the chase, you simply can't do it.

The @ElementCollection annotations expects a collection of embeddable types. This will be simply translated to a child table having a FK reference to the parent entity.

So the Embeddable maps the child SQL table columns. A List can't be an embeddable.

Because you cannot nest collections of embeddable types:

An embeddable class (including an embeddable class within another embeddable class) that is contained within an element collection must not contain an element collection, nor may it contain a relationship to an entity other than a many-to-one or one-to-one relationship. The embeddable class must be on the owning side of such a relationship and the relationship must be mapped by a foreign key mapping. (See Section 2.9.)

What you can do is to create a new entity type Child that has a List instead.

So the parent child embedable list becomes:

List<Child> children = new ArrayList<>();

Where the Child looks like:

public class Child {

    private Long id;

    List<String> children = new ArrayList<>();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download