Hi all,
I have a class Order which contains Products, which in turns contains Units (Unit is an abstract class, Copy and Folder are subclasses) :
Code:
@Entity
@Table(name = "t_order")
public class Order implements Serializable {
private static final long serialVersionUID = 8986089659809970850L;
@Id
@GeneratedValue
@Column(name = "order_id")
private Integer id;
@Column(unique = true, nullable = false)
private String orderNumber;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "order", fetch = FetchType.EAGER)
private Set<Product> products = new HashSet<Product>();
// setters, getters and so on (...)
}
Note that the collection products is eagerly fetched.
Code:
@Entity
@Table(name="t_product")
public class Product implements Serializable {
private static final long serialVersionUID = 6823234587691684869L;
@Id
@GeneratedValue
@Column(name = "product_id")
private Integer id;
@ManyToOne
@JoinColumn(name = "order_fk")
private Order order;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "product")
private Set<Unit> units = new HashSet<Unit>();
}
In this class, units is a lazy collection.
Code:
@Entity
@Table(name="t_unit")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Unit {
@Id
@GeneratedValue
@Column(name = "unit_id")
private Integer id;
private String unitNumber;
}
@Entity
@PrimaryKeyJoinColumn(name = "folder_id")
public class Folder extends Unit implements Serializable {
// (...)
}
@Entity
@PrimaryKeyJoinColumn(name = "copy_id")
public class Copy extends Unit implements Serializable {
// (...)
}
Well. Now, I would like to find the order which number is, say "74152", and I want to fetch the units along with the products :
Code:
HQL:
from Order o inner join fetch o.products p inner join fetch p.units where o.orderNumber = ?
I don't know if this is the right query. But I have another problem :
Quote:
org.hibernate.InstantiationException: Cannot instantiate abstract class or interface: com.xx.domain.Unit.
I don't understand why Hibernate tries to instanciate this class instead of using the concrete ones. Can anyone help me (it is not desirable to eargerly load units).
Thanks in advance !