Hibernate version: 3.0.5
I have two classes, A and B, which have a many-to-many relationship to each other on a join table. They are mapped and work fine. The relationship is mapped lazily, so when I call A.getBs() and iterate over the resulting collection, I get n+1 selects. I get the impression that this is the expected behaviour from the FAQ, which suggests I map the association eagerly.
This is not what I want however. An eager mapping implies that when A is loaded, all its Bs should be loaded at that same time. Instead, I want an A's Bs to remain unloaded until I actually call A.getBs(), at which point the entire collection and all the objects within it should be initialized. I tried calling Hibernate.initialize() on the collection, but that doesn't initialize the objects inside it. I can emulate the behaviour by using batch fetching and setting the batch fetch size to a large number (like say 1000), but then that's not perfect either. Hibernate generates SQL that uses the 'wrong' side of the association table resulting in hundreds of ?, ?, ?, ?... for each and every row being batch fetched. It makes sense in the context of batch fetching of course, but if I know that I will need the whole collection, I'd like to be able to avoid this.
On an unrelated note, is it possible to declare certain mapped classes as 'read only' and have Hibernate enforce their non-persistability?
|