0

I have a design model with 2 classes: Game and Highscore. The relationship between them is: a game can have 0 or many highscores. So, I'm trying to define class Highscore as @Embeddable and inside Game I write:

@Embedded
private List<Highscore> highscores = new ArrayList<Highscore>();

The problem is that I get the following error from Eclipse: java.util.List is not mapped as an embeddable.

Does anybody know what can be done?

Game:

@Entity
@Table(name="games")
public class Game {

    @Id
    private String name;

    @Column(name="highscore_table_size")
    private Integer highscoreTableSize;

    private String url;

    @Embedded
    private List<Highscore> highscores = new ArrayList<Highscore>();

    ...
}

Highscore:

@Embeddable
public class Highscore {

    private String playerName;

    private Date date;

    private int score;

    // getters and setter
}

EDIT: I also had a Table xyz cannot be resolved error from Eclipse and, searching for a solution, I've found this, and turning off the JPA validator for Build did the trick also for the error presented in this post.

Community
  • 1
  • 1
  • 1
    May be it did the trick, but it is not portable. The documentation tells it clear: @Embedded `defines a persistent field or property of an entity whose value is an instance of an embeddable class`, not of a collection. – V G Nov 23 '13 at 18:45
  • I changed to @ElementCollection. – Sorin Adrian Carbunaru Nov 25 '13 at 15:57

2 Answers2

1

You should use the @ElementCollection for that.

Excerpt from the documentation:

Defines a collection of instances of a basic type or embeddable class. Must be specified if the collection is to be mapped by means of a collection table.

V G
  • 18,822
  • 6
  • 51
  • 89
0

You should have a OneToMany association, not an Embedded.

Embedded is used to store the fields of an object in the same table as the fields of its owning entity. It doesn't make sense to use it with a List.

HighScore must be an entity, with an ID, and you must have a OneToMany association between both entities.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Yeah, I know I can make `Highscore` an entity, but Postgres has the `array` type and we want to test it at our department by having a list of highscores embedded in `Game` class. – Sorin Adrian Carbunaru Nov 23 '13 at 18:32