• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Question on Mapping Many To Many  RSS feed

 
Ranch Hand
Posts: 630
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Have a Many To Many Mapping Lets say Accounts and Funds. Which works, so when I bring back account, it will bring back all the funds associated with that account.

But there will be a case where I just want to bring back all the accounts only. I dont think there is but is there a @annotation based command, to say dont go down a level on a Mapping assocaition.

Otherwise, the other way is bring back the assocaition, and dont map them, or create a HQL to just bring back the accounts.

Thanks Tony
 
Rancher
Posts: 3931
45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Set your @ManyToMany association as FetchType.LAZY.
That way the Funds will only be fetched when they are accessed.
 
Tony Evans
Ranch Hand
Posts: 630
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave,

Thanks for that. So the Acount Entity is returned, but with fetch set to  FetchType.LAZY. I long as I dont access the set of Funds, thay are not fetched.

Thank you very much, you have been a great help to me.

Regards Tony Cavanagh.
 
Saloon Keeper
Posts: 20510
115
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lazy fetching is extremely useful, since some entities are linked in so many ways that an eager fetch could pull in anything up to and including the entire database!

One thing to be aware of, however, is that Lazy fetches can only be resolved while the entities are attached to the persistence manager. When I code webapps, the database logic detaches the set of objects that it returns to the higher levels of the webapp (for example, the GUI layer). There are several reasons why I prefer doing this, but among the top ones is that it ensures that I don't accidentally zap database data in non-database code. Instead I work with detached data and then when AND IF I want that data to update in the database, I call one of the database logic services that will then convert it to attached data and post the changes to the database server (and, incidentally, ensure that no other process has altered the data behind my back).

To ensure you have exactly what you need in a detached data set, simply force fetches before you detach. For example, if I want to return a working set of an account and its associated funds, but not any other objects that the funds might link to, I'd simply iterate the funds collection that was mapped @ManyToMany and have the iteration look at one of the fields in the funds records (the funds ID field is often a good choice, but any field local to the funds record will do).
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!