I am coming from eclipselink and trying to work on my own through Hibernate.
Suppose we have a class Car and a class Wheel . Car class has n wheels. Both objects are associated with a bi-directional association. More importantly, on the Wheel side, I have a Car link:
@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "car_id") private Car car;
plus the recipient.
Now I want to get the wheel using its identifier. From my EntityManager (non-sleep mode Session ). I initialize the EntityManager as follows:
EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); EntityManager em = emf.createEntityManager();
The next step is to select a wheel as follows:
Wheel wheel = em.find(Wheel.class, 1);
The wheel returns the desired class and its penalty. 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 be loaded. Instead of hibernate, the proxy class is loaded instead.
The only solution I have developed so far is to install FetchType.EAGER or directly get the connection. I realized that the SQL statement in Hibernate is still executing, but no real object has been delivered. Also after
Hibernate.initalize(car)
I can not get the car.
Is there a way to return the expected object without generating a query or impatient fetch?
source share