• 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Question on Mapping Many To Many  RSS feed

 
Ranch Hand
Posts: 616
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: 3797
40
  • 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: 616
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.
 
Bartender
Posts: 20125
103
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).
 
If you have a bad day in October, have a slice of banana cream pie. And this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!