I'm currently taking Hibernate for a spin, and have a question about design. I'm working on a screen which displays a list of contracts. Each contract has three associations, "buyer", "seller" and "broker". These are all instances of Company. In addition, each Company instance has a Country instance, and in my list I need to display the name of the country.
I've also added:
.setFetchMode("buyer", FetchMode.JOIN)
.setFetchMode("seller", FetchMode.JOIN)
.setFetchMode("broker", FetchMode.JOIN)
To the criteria I'm using to fetch these, so that these associations are eagerly fetched. I've noticed however that the country table is being repeatedly queried to get the name of the country (which is the only Country property shown in the view I'm working on). So I added this:
criteria.createCriteria("seller").setFetchMode("country", FetchMode.JOIN);
criteria.createCriteria("buyer").setFetchMode("country", FetchMode.JOIN);
criteria.createCriteria("broker").setFetchMode("country", FetchMode.JOIN);
To eagerly fetch the countries as well.
But what I really want is to have the entire graph initialized because I'm going to display values from all the associations anyway.
I've also considered creating a simpler version of the Contract object, because I really just need the name, and country name from each involved company. Basically I'd "flatten" the object graph into a more table-like structure, where instead of an entire company object I just have the company name and country as Strings. But I'm not exactly sure how I can do this, because it basically involves mapping up a query rather than a single table.
But I guess what I'm after is opinions, ideas, links to tutorials etc. about what is the best (or at least a good) approach to this sort of thing. I'm thinking it's not too uncommon to have fairly large objects and situations where you need to just list a small selection of the object properties for a large number of objects.
|