Under what circumstances will an entity be able to lazily load its relationships in JPA
- by Mowgli
Assuming a Java EE container is being used with JPA persistence and JTA transaction management where EJB and WAR packages are inside a EAR package.
Say an entity with lazy-load relationships has just been returned from a JPQL search, such as the getBoats method below:
@Stateless
public class BoatFacade implements BoatFacadeRemote, BoatFacadeLocal {
@PersistenceContext(unitName = "boats")
private EntityManager em;
@Override
public List<Boat> getBoats(Collection<Integer> boatIDs) {
if(boatIDs.isEmpty()) {
return Collections.<Boat>emptyList();
}
Query query = em.createNamedQuery("getAllBoats");
query.setParameter("boatID", boatIDs);
List<Boat> boats = query.getResultList();
return boats;
}
}
The entity:
@Entity
@NamedQuery(
name="getAllBoats",
query="Select b from Boat b where b.id in : boatID")
public class Boat {
@Id
private long id;
@OneToOne(fetch=FetchType.LAZY)
private Gun mainGun;
public Gun getMainGun() {
return mainGun;
}
}
Where will its lazy-load relationships be loadable (assuming the same stateless request):
Same JAR:
A method in the same EJB
A method in another EJB
A method in a POJO in the same EJB JAR
Same EAR, but outside EJB JAR:
A method in a web tier managed bean.
A method in a web tier POJO.
Different EAR:
A method in a different EAR which receives the entity through RMI.
What is it that restricts the scope, for example: the JPA transaction, persistence context or JTA transaction?