Under what circumstances will an entity be able to lazily load its relationships in JPA

Posted by Mowgli on Stack Overflow See other posts from Stack Overflow or by Mowgli
Published on 2013-10-26T03:51:49Z Indexed on 2013/10/26 3:53 UTC
Read the original article Hit count: 164

Filed under:
|
|
|

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):

  1. Same JAR:
    • A method in the same EJB
    • A method in another EJB
    • A method in a POJO in the same EJB JAR
  2. Same EAR, but outside EJB JAR:
    • A method in a web tier managed bean.
    • A method in a web tier POJO.
  3. 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?

© Stack Overflow or respective owner

Related posts about java-ee

Related posts about jpa