Consider the following model
@Entity
// JPA and JAXB annotations here
public class Employee implements Serializable {
// other fields, annotations, stuffs
...
@ElementCollection(fetch = FetchType.LAZY,
targetClass = Address.class)
@CollectionTable(name = "employee_address",
schema = "hris",
joinColumns = @JoinColumn(name = "employee_id",
nullable = false,
referencedColumnName = "employee_id",
foreignKey = @ForeignKey(ConstraintMode.CONSTRAINT)))
protected Set<Address> addresses;
// setters, getters
...
}
@Embeddable
// JAXB annotations here
public class Address implements Serializable {
// fields, setters, getters
}
The Address
class is annotated with @Embeddable
annotation, and the Employee
class has an embedded element collection of addresses
. The element collection's fetch
is set to FetchType.LAZY
. Now, I would like to create a @NamedQuery
that would retrieve all employees with addresses eagerly initialized. Knowing that JOIN FETCH
will only work with entity collections annotated with @OneToMany
or @ManyToMany
based on JPA 2.1, how would I create a valid JPQL
query that would allow me to eagerly retrieve embedded element collections?