Hi,
I use the single table strategy (with discriminator) to use inheritance
UML schema
I would like to fetch all the orders of a customer with all the associations related (cars, books and tvs). Do you know how to achieve this without breaking the model classes.
Code:
@Entity
@Table(name = "customers")
public class Customer{
private Date birthDate;
private String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer", cascade = CascadeType.ALL, orphanRemoval = true)
@OrderBy("order")
private List<? extends Order> orders;
}
@Entity
@Table(name = "orders")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public abstract class Order{
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@NotNull
@JoinColumn(name = "CustomerID")
private Customer customer;
}
@Entity
@DiscriminatorValue("book")
public class BookOrder {
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name="book_id")
private Set<Book> books;
}
@Entity
@DiscriminatorValue("car")
public class CarOrder {
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name="car_id")
private Set<Car> cars;
}
@Entity
@DiscriminatorValue("tv")
public class TvOrder {
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name="tv_id")
private Set<Tv> tvs;
}
if im doing in hql
Code:
select cutomer from customer customer
inner join fetch customer.orders order
left join fetch order.cars
left join fetch order.books
left join fetch order.tvs
Im getting the error
org.hibernate.QueryException: could not resolve property: cars, and it makes sense in the abstract class Order this field doesnt exist.
Do you know how i can achieve this ? what is the recommendation of hibernate in this case ?
My goal is to simple a simple query and to fetch everything