i have a hard time understanding the lazy fetching since i does not work for as i read about it in a book, they say that in lazy fetching jpa will load the entities only when they are accessed through geters so i created an Arquillian project to test this concept but it does not work. here are my two entities
Person
package com.actionbazaar.model;
@Entity
@TableGenerator(
initialValue = 5,
name = "PERSON_SEQ",
table = "PERSON_SEQ_TABLE",
pkColumnName = "SEQ_NAME",
pkColumnValue = "PERSON",
valueColumnName = "SEQ_VALUE")
public class Person implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String fname;
private String lname;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "owner", cascade = CascadeType.PERSIST)
List<Address> addresses;
//getters and setters
}
Address
@Entity
public class Address implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String city;
private String zip;
private String street;
@ManyToOne
private Person owner;
//getters and setters
}
I have a stateless session bean with this method
public Person getFirstPerson() {
Person p = em.find(Person.class, 1);
em.detach(p);
//why this call does not create an exception
p.getAddresses().get(0);
return p;
}
since i detached the entity before accessing the address the addresses list should be empty and when i hace detached it so it is no longer managed by the entitymanager so i should not get addresses for the person the problem is that i can fetch the addresses of that person even i have lazy fetch for addresses field and detached the entity before accessinbg the addresses field!!!! pleas some have the explanation .
An other test
Person p= myStatlessSessionBean.getFirstPerson();
myOtherStalessSesionBean.moveAllPeopleToCity("NY");
if(p.getAddresses().get(0).getCity().equals("NY"))
{
system.out.prinln("person moved");
}
else {
system.out.prinln("person did not move");
} //prompts person did not move