Tim Visser
Tim Visser

Reputation: 923

Java Ebean save list to database

I have the following code:

@Entity
public class Album extends Model {

    /**
     * Generated UID
     */
    private static final long serialVersionUID = 7954535168852819314L;


    @Id
    public Long id;

    @Required
    public String name;


    public static Finder<Long, Album> find = new Finder<Long, Album>(Long.class, Album.class);
}

I need an Album to have a list of Songs. can I just add public List<Song> songs?

Is it a better practice to just have an entity Song then add a collection in the Album Entity ?

Upvotes: 1

Views: 997

Answers (1)

biesior
biesior

Reputation: 55798

Almost... to make it working you need to add annotation with relation type, Many songs belongs to One Album add this field to the Song model:

@ManyToOne
public Album album;

So in your Album model you can add reverse field:

@OneToMany(mappedBy = "album")
public List<Song> songs;

Be careful about mappedBy - it need to have the same value as the field added to the Song model.

Upvotes: 2

Related Questions