I have books and movies that are stored in the regarding shelfs: "book shelf" and "movie shelf".
Abstract Shelf<T>:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
@Table(name = "shelf")
public abstract class Shelf<T extends ShelfObject<?>> {
...
@Column(name = "type", nullable = false, insertable = false, updatable = false)
@Enumerated(EnumType.STRING)
private ShelfType streamType;
@OneToMany(mappedBy = "shelf", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<T> shelfObjects;
}
And two shelfs:
@Entity
@DiscriminatorValue(ShelfType.Stringified.BOOK_SHELF)
public class BookShelf extends Shelf<BookShelfObject> {
}
@Entity
@DiscriminatorValue(ShelfType.Stringified.MOVIE_SHELF)
public class MovieShelf extends Shelf<MovieShelfObject> {
}
Abstract ShelfObject<T>:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "shelf_object")
public abstract class ShelfObject<T extends Shelf<?>> {
@ManyToOne
@JoinColumn(name = "shelf_id", nullable = false)
private T shelf;
}
And two shelf objects:
@Entity
public class BookShelfObject extends ShelfObject<BookShelf> {
}
@Entity
public class MovieShelfObject extends ShelfObject<MovieShelf> {
}
In the business logic I will use only BookShelf with regarding BookShelfObject entities and MovieShelf only with MovieShelfObject entities. But Hibernate told me that
ShelfObject.shelfhas an unbound type and no explicit target entity. Resolve this Generic usage issue or set an explicit target attribute (eg.,@OneToMany(target=)or use an explicit@Type.
So, may you help me to achieve this goal?
I want to keep generics to prevent code duplication such as:
@Entity
public class BookShelfObject extends ShelfObject {
@ManyToOne
@JoinColumn(name = "shelf_id", nullable = false)
private BookShelf shelf;
}
@Entity
public class MovieShelfObject extends ShelfObject {
@ManyToOne
@JoinColumn(name = "shelf_id", nullable = false)
private MovieShelf shelf;
}