I'm coming from eclipselink and try to work myself through Hibernate.
Lets assume we have a class Car and a class Wheel. The Car class has n wheels. Both entities are connected with a bidirectional association. More importantly on the Wheel side I have a Car reference:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "car_id")
private Car car;
plus a getter.
Now I want to fetch a wheel using its id. From my EntityManager (not a hibernate Session). I initialize the EntityManager like this:
EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = emf.createEntityManager();
The next step is to fetch a wheel like this:
Wheel wheel = em.find(Wheel.class, 1);
The wheel return the wanted class and its fine. Now I want to know which car is the parent of the wheel with something like:
Car car = wheel.getCar();
With eclipselink, the actual car would have been loaded. With hibernate a proxy class is loaded instead.
The only solution I worked out so far is to set the FetchType.EAGER or directly fetch join the relationship. What I realized is that the SQL statement in Hibernate is still executed, but no real object delivered. Also after
Hibernate.initalize(car)
I cannot retrieve the car entity.
Is there any way to get the expected object back without forming a query or an eager fetch?