-->
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: Eager loading of objects with nested children relationships
PostPosted: Mon Jan 28, 2013 2:22 pm 
Newbie

Joined: Mon Jan 28, 2013 10:52 am
Posts: 1
Hi All,
I am trying to find out if I can start using in deep hibernate for orm mapping within my organization.

I don't think we have very strange needs, only in some situations we need to do huge eager loading in
structures similar to the one appended at the end. Its a master child structure where children do have
additional nested children levels.

Due to the structure of the posted tables we have developed a reasonably fast and not too resource
consuming loading method than makes one query per table, and later it makes a post-processing to eagerly rebuild
the objects in memory.

Code:
select * from PLANET
   order by PLANET_ID;

select * from SATELLITE
   order by PLANET_ID, SATELLITE_ID;

select * from STAR
   order by PLANET_ID, STAR_ID;

select * from CONTINENT
   order by PLANET_ID, CONTINENT_ID;

select * from CONTINENT_REGION
   order by PLANET_ID, CONTINENT_ID, REGION_ID;

select * from COUNTRY
   order by PLANET_ID, CONTINENT_ID, COUNTRY_ID;

select * from COUNTY
   order by PLANET_ID, CONTINENT_ID, COUNTRY_ID, COUNTY_ID;

select * from ROAD
   order by PLANET_ID, CONTINENT_ID, COUNTRY_ID, COUNTY_ID, ROAD_ID;


in practice queries for "lower level" tables are loaded keeping same order as their master for foreign
keys and adding additional order for local keys.
As a result we get a set of recordsets that are all "lexicographically" ordered in an homogeneous
way with respect to their parent (if any).
This gives us the ability to fetch the data as follows (single pass)

Code:
run all queries at this point...

cycle p on PLANET {

    // load planet fields...
   
   cycle cnt on CONTINENT while cnt.CONTINENT_FK_PLANET = p.PLANET_PK {

     // load continent fields...
    
     cycle cnt_r on CONTINENT_REGION while cnt_r.CONTINENT_REGION_FK_CONTINENT = cnt.CONTINENT_PK {
      // load continent_region fields...
     }

     cycle cntry on COUNTRY while cntry.COUNTRY_FK_CONTINENT = cnt.CONTINENT_PK {

      // load country fields...
      
      cycle cnty on COUNTY while cnty.COUNTY_FK_COUNTRY = cntry.COUNTRY_PK {

         // load county fields...
         
         cycle rd on ROAD while rd.ROAD_FK_COUNTY = cnty.COUNTY_PK {
            // load road fields...
         }
      }
     }
   }

   cycle sat on SATELLITE while sat.SATELLITE_FK_PLANET = p.PLANET_PK {
      // load sat fields...
   }
   
   cycle str on STAR while str.STAR_FK_PLANET = p.PLANET_PK {
      // load str fields...
   }
}

close all queries at this point...


Is there any way to obtain something similar in hibernate, or some way to implement this behavior?
I would really be glad also to hack hibernate source code if this is necessary, maybe someone else
out there might take advantage of this.

Thanks for the support
Tomaso Tosolini

-------------------8<--------------------------------------

Code:
DROP TABLE "TOWN";
DROP TABLE "COUNTY";
DROP TABLE "CONTINENT_REGION";
DROP TABLE "COUNTRY";
DROP TABLE "STAR";
DROP TABLE "SATELLITE";
DROP TABLE "CONTINENT";
DROP TABLE "PLANET";

-- |||||||||||||||||||||||||||||||||||||||||||||| --

CREATE TABLE "PLANET"
  (
    "PLANET_ID"   VARCHAR2(20 BYTE) NOT NULL,
    "NAME"       VARCHAR2(20 BYTE) NOT NULL,
    CONSTRAINT "PLANET_PK" PRIMARY KEY ("PLANET_ID")
);

CREATE TABLE "CONTINENT"
  (
    "PLANET_ID"    VARCHAR2(20 BYTE) NOT NULL,
    "CONTINENT_ID"   VARCHAR2(20 BYTE) NOT NULL,
    "NAME"          VARCHAR2(20 BYTE) NOT NULL,
    CONSTRAINT "CONTINENT_PK" PRIMARY KEY ("PLANET_ID", "CONTINENT_ID"),
    CONSTRAINT "CONTINENT_FK_PLANET" FOREIGN KEY ("PLANET_ID") REFERENCES "PLANET" ("PLANET_ID")
);

CREATE TABLE "SATELLITE"
  (
    "PLANET_ID"    VARCHAR2(20 BYTE) NOT NULL,
    "SATELLITE_ID"   VARCHAR2(20 BYTE) NOT NULL,
    "NAME"          VARCHAR2(20 BYTE) NOT NULL,
    CONSTRAINT "SATELLITE_PK" PRIMARY KEY ("PLANET_ID", "SATELLITE_ID"),
    CONSTRAINT "SATELLITE_FK_PLANET" FOREIGN KEY ("PLANET_ID") REFERENCES "PLANET" ("PLANET_ID")
);

CREATE TABLE "STAR"
  (
    "PLANET_ID"    VARCHAR2(20 BYTE) NOT NULL,
    "STAR_ID"   VARCHAR2(20 BYTE) NOT NULL,
    "NAME"          VARCHAR2(20 BYTE) NOT NULL,
    CONSTRAINT "STAR_PK" PRIMARY KEY ("PLANET_ID", "STAR_ID"),
    CONSTRAINT "STAR_FK_PLANET" FOREIGN KEY ("PLANET_ID") REFERENCES "PLANET" ("PLANET_ID")
);


CREATE TABLE "COUNTRY"
  (
    "PLANET_ID"    VARCHAR2(20 BYTE) NOT NULL,
    "CONTINENT_ID"   VARCHAR2(20 BYTE) NOT NULL,
    "COUNTRY_ID"    VARCHAR2(20 BYTE) NOT NULL,
    "NAME"          VARCHAR2(20 BYTE) NOT NULL,
    CONSTRAINT "COUNTRY_PK" PRIMARY KEY ("PLANET_ID", "CONTINENT_ID", "COUNTRY_ID"),
    CONSTRAINT "COUNTRY_FK_CONTINENT" FOREIGN KEY ("PLANET_ID", "CONTINENT_ID") REFERENCES "CONTINENT" ("PLANET_ID", "CONTINENT_ID")
  );

CREATE TABLE "CONTINENT_REGION"
  (
    "PLANET_ID"    VARCHAR2(20 BYTE) NOT NULL,
    "CONTINENT_ID"   VARCHAR2(20 BYTE) NOT NULL,
    "REGION_ID"    VARCHAR2(20 BYTE) NOT NULL,
    "NAME"          VARCHAR2(20 BYTE) NOT NULL,
    CONSTRAINT "CONTINENT_REGION_PK" PRIMARY KEY ("PLANET_ID", "CONTINENT_ID", "REGION_ID"),
    CONSTRAINT "CONTINENT_REGION_FK_CONTINENT" FOREIGN KEY ("PLANET_ID", "CONTINENT_ID") REFERENCES "CONTINENT" ("PLANET_ID", "CONTINENT_ID")
  );
 
CREATE TABLE "COUNTY"
  (
    "PLANET_ID"    VARCHAR2(20 BYTE) NOT NULL,
    "CONTINENT_ID"   VARCHAR2(20 BYTE) NOT NULL,
    "COUNTRY_ID"    VARCHAR2(20 BYTE) NOT NULL,
    "COUNTY_ID"    VARCHAR2(20 BYTE) NOT NULL,
    "NAME"          VARCHAR2(20 BYTE) NOT NULL,

    CONSTRAINT "COUNTY_PK" PRIMARY KEY ("PLANET_ID", "CONTINENT_ID", "COUNTRY_ID", "COUNTY_ID"),
    CONSTRAINT "COUNTY_FK_COUNTRY" FOREIGN KEY ("PLANET_ID", "CONTINENT_ID", "COUNTRY_ID") REFERENCES "COUNTRY" ("PLANET_ID", "CONTINENT_ID", "COUNTRY_ID")
  );

CREATE TABLE "ROAD"
  (
    "PLANET_ID"    VARCHAR2(20 BYTE) NOT NULL,
    "CONTINENT_ID"   VARCHAR2(20 BYTE) NOT NULL,
    "COUNTRY_ID"    VARCHAR2(20 BYTE) NOT NULL,
    "COUNTY_ID"     VARCHAR2(20 BYTE) NOT NULL,
    "ROAD_ID"     VARCHAR2(20 BYTE) NOT NULL,
    "NAME"          VARCHAR2(20 BYTE) NOT NULL,

    CONSTRAINT "ROAD_PK" PRIMARY KEY ("PLANET_ID", "CONTINENT_ID", "COUNTRY_ID", "COUNTY_ID", "ROAD_ID"),
    CONSTRAINT "ROAD_FK_COUNTY" FOREIGN KEY ("PLANET_ID", "CONTINENT_ID", "COUNTRY_ID", "COUNTY_ID") REFERENCES "COUNTRY" ("PLANET_ID", "CONTINENT_ID", "COUNTRY_ID", "COUNTY_ID")
  );

-------------------8<--------------------------------------

with RUBY syntax:

Code:
planet :has_many continents
planet :has_many satellites
planet :has_many stars (just saw it in Star Wars so it's true)
continent :has_many countries
continent :has_many continent_regions
country :has_many counties
county :has_many roads


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.