Versions: Hibernate 3.2, PostgreSQL 8.1
Gist of problem: Trying to fetch as precisely as possible specific branches of my object graph, three levels deep. There are
maps involved...
A sketch of the objects directly involved:
Code:
/** @hibernate.class table="models" */
public class Model {
private Long id;
private Set<Part> parts = new HashSet<Part>();
public Model() {}
/** @hibernate.id generator-class="sequence" column="id" */
public Long getId() { return this.id; }
protected void setId(Long id) { this.id = id; }
/**
* @return a Set of the constituents (the Parts) of this particular Model object.
* @hibernate.set table="models_parts" lazy="true"
* @hibernate.collection-key column="model"
* @hibernate.collection-many-to-many class="Part" column="part"
*/
public Set<Part> getParts() { return this.parts; }
public void setPart(Set<Part> parts) { this.parts = parts; }
}
Code:
/** @hibernate.class table="parts" */
public class Part {
private Long id;
private Map<Region, String> serialMap = new RegionalMap();
// Region is an Enum, and RegionalMap is an EnumMap<Region, String>
public Part() { }
/** @hibernate.id generator-class="sequence" column="id" */
public Long getId() { return this.id; }
protected void setId(Long id) { this.id = id; }
/**
* @return a RegionalMap of this Part's serial number for each Region.
* @hibernate.map table="parts_r"
* @hibernate.collection-key column="part"
* @hibernate.collection-index column="region" type="regionenumtype"
* @hibernate.collection-element column="serial_nr" type="string" not-null="true"
*/
public Map<Region, String> getSerialMap() { return this.serialMap; }
public void setSerialMap(Map<Region, String> nameL) { this.serialMap = serialMap; }
/* the SerialNr for the given Region. */
public String getSerialNr(Region region) {
return serialMap.get(region);
}
}
Setup: At the time the request is made, I know specifically which
Region I'll have to deal with. I will need to display one
Model with all its
Parts, and each
Part's
serialNr for the given
Region.
Code between sessionFactory.openSession() and session.close():Code:
HibernateTemplate template = getHibernateTemplate();
Model model = (Model) template.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Model aModel = (Model) session.get("Model", id);
Hibernate.initialize(aModel.getParts());
return aModel;
}
});
So far, the
Hibernate.initialize fetches only the
Parts, but these aren't initialized. What I would want is some sort of:
Code:
for (Part part : parts)
aModel.part.getSerialNr(currentRegion);
Questions:- Is there a way of including all the bits I want to fetch within one HQL query, all the way down to the third layer?
- Alternatively, is there a way of initializing the last bit without an explicit call to getSerialNr(currentRegion)?
Note: I know this is a contrived example; this is not the actual situation I have to deal with; there is a lot more data than a few serial numbers! Please play along!