• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[Hibernate] about loading associations

 
Nicolas Daudin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i have a question about Hibernate

I have a service called StatisticService that has to handle entities of class Route.
I did a routeDao.loadAll() to get all the Routes from my base.
Then, i go to a POJO (i.e. no longer in any Hibernate session), and there, i do a myRoute.getPoint(). Route and Point are linked in the base, so are they in Hibernate. When i do this, thus i want to get the corresponding instance of Point. But problem is, i get a Point$$EnhancedByCGLIB$$ instance.

So here is my question:
if i am requesting on the entities in my DB, do i need to be in a service (i.e. in a hibernate session)??? I thought that getting my Routes through my DAO was enough to get all its dependencies and so to be disconnected from Hibernate.

PS: its just to do some read... i dont want to write on those instances...

Nicolas
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lazy initialization means that an associated object is only populated when a property of that object is accessed, otherwise you just get a proxy. So if you have this sort of association between Route and Point, you will need to call route.getPoint().getWhatever() to populate Point. Alternatively, if you know you need Point, you could use eager fetching in a query. If you always need Point you could define the relationship without the lazy association (this is generally not a good thing to do though).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic