Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing arraylist in a mysql database using JPA

I am trying to add an arraylist of double values to my mysql database using JPA and Hibernate but it is not storing the values as doubles, it is storing the arraylist object and then a lot of characters.

The image below is what the database looks like.

Arraylist

Here's my code:

Playlist.java

@Entity
public class Playlist {

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

private String name, persistentId;
private double playlistId;
@ElementCollection
@CollectionTable(name ="tracks")
private ArrayList<Double> tracks = new ArrayList<Double>();

@OneToMany(mappedBy="playlist") 
Set<Song> songs;

public Playlist(String name, String persistentId, double playlistId, ArrayList<Double> tracks) {
    this.name = name;
    this.persistentId = persistentId;
    this.playlistId = playlistId;
    this.tracks = tracks;
}

public Playlist() {
}

Main.java

Playlist p = new Playlist();
ArrayList<Double> tracks = new ArrayList<Double>();
        for (int j = 0; j < tList.getLength(); ++j) {
            Element track = (Element) tList.item(j);

            Double trackId = (Double) x.evaluate("key[.='Track ID']/following-sibling::integer[1]", track,
                    XPathConstants.NUMBER);
            tracks.add(trackId);
            p.setTracks(tracks);

        }

Could anyone tell me what I'm doing wrong please?

like image 886
ariella123 Avatar asked May 10 '26 01:05

ariella123


1 Answers

Try to set more informations :

@ElementCollection
@CollectionTable(name ="tracks" , joinColumns=@JoinColumn(name="playlist_id"))
@Column(name="track")
private List<Double> tracks = new ArrayList<>();
like image 82
Mr_Thorynque Avatar answered May 12 '26 15:05

Mr_Thorynque



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!