Yeynno
Yeynno

Reputation: 331

Hibernate, OneToMany, AnnotationException

I have a problem with OneToMany annotation

@Entity
@Table(name = "RESULT_HISTORY")
public class ResultHistoryImpl implements ResultHistory, Serializable {

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


    @OneToMany
    private final Set<Game> games = new HashSet<>();


...

}

and class

@Entity
@Table(name = "GAME")
public class GameImpl implements Game, Serializable {

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

    ...
}

I have more fields, setters/getters and constructors

I use spring and in my configuration files I have packagesToScan where I put packages of those classes

The problem is that I get org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class

I read many topics on stackoverflow, so the problem isn't using org.hibernate.annotation etc

Upvotes: 1

Views: 457

Answers (3)

V G
V G

Reputation: 19002

What you tried is not possible. Game is an interface and you cannot map an interface to DB, because hibernate does not know which implementation to use. But what you can do, is

  1. use in all your entity classes the GameImpl class directly. I mean correct your ResultHistoryImpl class to this: @OneToMany private final Set<GameImpl> games = new HashSet<GameImpl>();
  2. If you want to have another abstraction, add an abstract class AbstractGame implements Game and annotate it with @Entity and use it instead of the Game interface.

Upvotes: 3

kavai77
kavai77

Reputation: 6626

For @OneToMany you have to provide a mappedBy property which points to a field name in the other class. In your case:

public class ResultHistoryImpl {
...
    @OneToMany(mappedBy = "resultHistory")
    private final Set<GameImpl> games = new HashSet<>();
}

public class GameImpl {
    @ManyToOne
    @JoinColumn(name = "result_history")
    private ResultHistoryImpl resultHistory;
}

Upvotes: 0

Radek Wroblewski
Radek Wroblewski

Reputation: 299

Use @MappedSuperclass annotation on Game.

Upvotes: 0

Related Questions