Hai due modi per risolverla mantenendo il fetch lazy:
1) Come mostrato nell'esempio ti tieni traccia della sessione e prima di chiuderla richiami il getXXX della tua property
2) Imposti la query in modo che effettui il fetch di quella relazione.
Questo è un esempio di valorizzazione della relazione tramite query:
Il "trucco" sta nelle parole chiave "left join fetch chd.toyList" dove dico di valorizzare la lista dei giocattoli.codice:@Entity @Table(name = 'CHILD') @NamedQuery(name = 'findChildByName', query = 'select DISTINCT(chd) from Child chd left join fetch chd.toyList where chd.childName=:chdName') public class Child { public static interface Constants { public static final String FIND_CHILD_BY_NAME_QUERY = 'findChildByName'; public static final String CHILD_NAME_PARAM = 'chdName'; } @Id @GeneratedValue(strategy = GenerationType.AUTO) /** * The primary key of the CHILD table */ private Long childId; @Column(name = 'CHILD_NAME') /** * The name of the child */ private String childName; @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) /** * The toys the child has. We do not want the child to have the same toy more than * once, so we have used a set here. */ private Set<Toy> toyList = new HashSet<Toy>(); public Long getChildId() { return childId; } public void setChildId(Long childId) { this.childId = childId; } public String getChildName() { return childName; } public void setChildName(String childName) { this.childName = childName; } public Set<Toy> getToyList() { return toyList; } public void addToy(Toy toy) { toyList.add(toy); } }

Rispondi quotando