I created the entity Org
that has a one to one mapping with OrgHV
@Table(name="ORG_CHART")
@Entity
public class Org {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="ORG_ID")
private int id;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "ORG_ID")
private OrgHV orgHV;
//getters and Setters
}
In the service I retrieved all entities of Org
using Spring data Jpa repo and because of lazy loading OrgHV
will not be fetched until I call org.getOrgHV()
. The problem is I am doing so after I called repo.findAll()
, so the session/transaction is closed (correct me if I am wrong)? Why the code is working fine, why LazyInitializationException
is not thrown?
public orgService()
{
List<Org> orgs = orgRepo.findAll();
for (Org org: orgs)
{
orgRepo.findAll();
OrgHV orgHv = org.getOrgHV();
System.out.pringLn(orgHv.getId());
}
}