0

I'm trying to create a database table which has three empty arrays.

I want each array to start empty and add to them later, they will contain URLs in string format.

The problem I get is Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: moods, for columns: [org.hibernate.mapping.Column(images)]

I want to be able to retrieve an array of strings from this database table and then pick a random item from that array.

There are no table-joins in this database.

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "moods")
public class Mood implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "quotes")
    private List<String> quotes;

    @Column(name = "images")
    private List<String> images;

    @Column(name = "videos")
    private List<String> videos;

    public Mood(String name) {
        this.name = name;
        this.quotes = new ArrayList<>();
        this.images = new ArrayList<>();
        this.videos = new ArrayList<>();
    }

    public Mood() {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getQuotes() {
        return quotes;
    }

    public void setQuotes(List<String> quotes) {
        this.quotes = quotes;
    }

    public List<String> getImages() {
        return images;
    }

    public void setImages(List<String> images) {
        this.images = images;
    }

    public List<String> getVideos() {
        return videos;
    }

    public void setVideos(List<String> videos) {
        this.videos = videos;
    }

    public void addQuote(String quote) {
        this.quotes.add(quote);
    }

    public void addImage(String image) {
        this.images.add(image);
    }

    public void addVideo(String video) {
        this.videos.add(video);
    }

}

I expect to be able to retrieve this database table as an object and access each of the arrays as ArrayList of Strings.

The actual result is a mapping exception.

INDRAJITH EKANAYAKE
  • 3,894
  • 11
  • 41
  • 63
Ewan
  • 87
  • 1
  • 10
  • You can't save a list of objects to a field. Think about it, could you hold more than one thing in a single cell? nope. If you don't want to create separate entities for Quotes, Images and Videos and use ORM to map them, then your, probably only, option is to declare those properties as String, use a delimiter to create a String for each list and then save it. Respectively, you would use a split method after selection to split the String back to a list. There is a duplicate question, take a look https://stackoverflow.com/questions/48801080/save-arrayliststring-to-persist – Christos Karapapas Apr 13 '19 at 21:50
  • Possible duplicate of [Save ArrayList to persist](https://stackoverflow.com/questions/48801080/save-arrayliststring-to-persist) – Christos Karapapas Apr 13 '19 at 21:51
  • @ChristosK. not duplicate. This is about JPA, that one was for Android's API. – coladict Apr 14 '19 at 22:03
  • @ChristosK. You are right, the table requires a reference to another table where each element in that table represents an element in the array. In other words: its possible to have empty arrays, but the column must be referencing foreign keys. – Ewan Apr 15 '19 at 12:22

0 Answers0