I would like to use Hibernate associations in a way that appears to differ from the intended usage pattern. After reading the Hibernate docs and forums, and a good deal of experimenting, I have concluded that Hibernate aims to "fully manage" associations. In particular, if you load an object, Hibernate will either immediately or lazily load associated entities.
I'd like to instead declare associations that give me full control over loading, saving/updating, and deleting the associated entities. I don't want Hibernate to be "smart" and try to help me out for these associations -- although in other parts of our application, we love its intelligence. I want "Hibernate Light" :)
For context, I'm implementing a relatively flexible data/object model for hierarchies, roughly following the organizational pattern described by Martin Fowler:
http://www.martinfowler.com/apsupp/accountability.pdf.
At the heart of my model are Nodes and Relationships. Each relationship has a foreign key to a Parent and a Child node, as well as metadata such as the relationship type. Individual nodes might be related to thousands of other nodes.
I considered using Hibernate Collections to model the association of nodes to other nodes via relationships, but they don't appear to be flexible enough for my needs: we need to query for children based on relationship type, on attributes of the related nodes, to query for entire "subtrees" at once, to load only the "first 100 children", etc. Collections did not seem flexible/powerful enough to do this work efficiently in the database, although they'd be a great aid for managing small objects graphs in the JVM. We were ruminating that it would be nice if there were a facility "parameterize" Collections at runtime with "filters" (subqueries).
I also considered using Proxies, but lazy loading really doesn't address the problem. In fact, it usually makes things worse as our goal is to minimize the number of queries required to load specific subsets of a hierarchy graph.
The problem with straightforward many-to-one associations is that Hibernate insists on doing outer joins on all associations. (Things are even worse if outer joins are disabled, as Hibernate just falls back on separatequeries.) So if, for instance, I want to query for all "child" relationships and "child nodes" of a specific parent node, and the Relationship mapping declares many-to-one mappings to its parent and child Nodes, the query Hibernate executes includes two outer joins: one to fetch the relationship's children and another to fetch its parent. This happens even though I already have the (single) parent node. In addition to being an overly complex query, the result set is much larger than it needs to be because it includes numerous unwanted copies of the parent node. Finally, once the hierarchy object graph has been reconstituted in the JVM, I don't want the relationship objects to have references to distinct copies of the same parent node; I want them to all refer to a single parent node object.
So why do I want to declare these associations? So that I can utilize them in Hibernate queries that we hand code to exploit the very nifty "fetch join" feature. But the "side effects" of declaring the associations are pushing my team towards not using them -- we'll just have to construct the object graph ourselves from Lists of nodes and relationships returned by Hibernate.
I hope this rather lengthy inquiry makes sense -- I'm not clear if I just don't understand things or if I'm making a feature request. Any help is much appreciated!