Reputation: 331
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
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
GameImpl
class directly. I mean correct your ResultHistoryImpl
class to this: @OneToMany private final Set<GameImpl> games = new HashSet<GameImpl>();
AbstractGame implements Game
and annotate it with @Entity
and use it instead of the Game
interface.Upvotes: 3
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