-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Query fetching three levels deep, involving a Map
PostPosted: Tue Nov 28, 2006 3:25 am 
Newbie

Joined: Mon Aug 14, 2006 8:06 pm
Posts: 18
Location: Montréal, Québec
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:
  1. 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?
  2. 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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.