Some JPA provider like Hibernate uses Proxy to handle lazy initialization. Consider the following example:
@Entity
public class Person {
@Id
private Long id;
@ManyToOne(fetch=FetchType.LAZY)
private House house;
}
@Entity
public class House {
@Id
private Long id;
@Embedded
private Address address;
}
When fetching a Person entity, its house property is set to a Proxy (lazy).
Person person = em.find(Person.class, 1);
House house = person.getHouse(); // Proxy
if (house == null)
System.out.println("has no house);
else
System.out.println("has a house");
If the person does not have a house, the person object has a Proxy of house (not null). The code above will print wrong message. Is this an issue for JPA proxy?