How do I get a collection of related entities?

Posts   
 
    
Posts: 6
Joined: 07-Nov-2008
# Posted on: 07-Nov-2008 02:06:25   

hello!

I'm trying to figure out how to get a collection of entities that are involved in a join.

I have a table called UniversityLocation that ties to a table called ZipCodes through a table called UniversityLocationZipCode. (schema pic attached)

I cant figure out the syntax. Could anyone please point me in the right direction to not only check to see if there ARE any ZipCode records for the universitylocation I'm working with, but also, how to SAVE them!

Thanks!

USING: version 2.6 FINAL

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 07-Nov-2008 10:28:12   

I'm trying to figure out how to get a collection of entities that are involved in a join.

I'm not sure I 100% understand your question, but if you are asking about how to fetch the ZipCodes, the answer is to use PrefetchPath or rely on LazyLoading in Case of SelfServicing.

but also, how to SAVE them!

Please check the code example of the question: How do I create a m:n relation between two entity objects?

Posts: 6
Joined: 07-Nov-2008
# Posted on: 07-Nov-2008 19:27:12   

Hi there,

thanks for the reply, but the example doesn't work in my senario:

to Recap:

I need to populate an intermediate entity.

I have a UniversityLocation Entity (all FKs populated) I have a ZipCode Entity (no FKs)

and I need to populate the intermediate entity UniversityLocationZipCode, which has it's own PK, and the FKs from UniversityLocation and ZipCode.

(please see the attched schema screenshot)

I tried the following code:

UniversityLocationZipCodeEntity univLocZipCodeEnt = new UniversityLocationZipCodeEntity(); univLocZipCodeEnt.UniversityLocation = UniversityLocation; univLocZipCodeEnt.ZipCode = zipCode; univLocZipCodeEnt.Save(true);

the error that this code produced says I cannot create the intermediate entity: UniversityLocationZipCodeEntity because it can't find the universitylocationid in the UniversityLocationEntity... but my UniversityLocationEntity does indeed have a universitylocationID....

Must I instead use code like the following and explicitly supply the FKs?

UniversityLocationZipCodeEntity univLocZipCodeEnt = new UniversityLocationZipCodeEntity(); univLocZipCodeEnt.Universitylocationid = UniversityLocation.Universitylocationid; univLocZipCodeEnt.Zipcodeid = zipCode.Zipcodeid; univLocZipCodeEnt.Save(true);

thanks, Dustin

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 08-Nov-2008 05:38:34   
UniversityLocationZipCodeEntity univLocZipCodeEnt = new UniversityLocationZipCodeEntity();
univLocZipCodeEnt.UniversityLocation = UniversityLocation;
univLocZipCodeEnt.ZipCode = zipCode;
univLocZipCodeEnt.Save(true);

At that code, could you show us how you create _UniversityLocation _entity? UniversityLocation.IsNew is true? UniversityLocation.Universitylocationid is an identity column?

David Elizondo | LLBLGen Support Team
Posts: 6
Joined: 07-Nov-2008
# Posted on: 10-Nov-2008 17:07:33   

I make a UniversityLocationEntity with this code:

UniversityLocationEntity uniLocation = new UniversityLocationEntity();

            uniLocation.Universityid = Int32.Parse(ddlUniversity.SelectedValue);
            uniLocation.Universitytypeid = Int32.Parse(ddlUniType.SelectedValue);
            uniLocation.Universitylocationinfoid = SessionRetriever.UniversityLocationInfo.Universitylocationinfoid;
            uniLocation.Locationname = txtNewUnivLocationName.Text;
            uniLocation.Campaignid = Int32.Parse(ddlCampaign.SelectedValue);
            uniLocation.Zipcodetargeted = bool.Parse(rblZipTargeted.SelectedValue);
            uniLocation.Zipcodetagetedexclusive = bool.Parse(rblZipExclusive.SelectedValue);
            uniLocation.Statetargeted = bool.Parse(rblStateTargeted.SelectedValue);
            uniLocation.Statetargetedexclusive = bool.Parse(rblStateExclusive.SelectedValue);
            uniLocation.Customform = bool.Parse(rblCustomForm.SelectedValue);
            uniLocation.Advertiserid = Int32.Parse(ddlAdvertiserID.SelectedValue);
            uniLocation.Gradyearrange = ddlGradYearFrom.SelectedValue + "-" + ddlGradYearTo.SelectedValue;
            uniLocation.Offerid = txtOfferID.Text;
            uniLocation.Htmlpagetitle = txtHTMLPageTitle.Text;

            uniLocation.Save();

So can you please answer my question about why I need to explicitly set PKs in the intermediate entity rather than use the code in the example provided?

thanks, Dustin

Posts: 6
Joined: 07-Nov-2008
# Posted on: 10-Nov-2008 17:10:13   

Yes, if you can see the attached screenshot, UniversityLocationID is indeed a primary column.

as for UniversityLocation.IsNew is true? I did not set that property, but at the point I need to make a intermediate entity, my UniversityLocationEntity has already been saved to the database successfully.

thanks, Dustin

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 10-Nov-2008 17:26:59   

redbullchronic wrote:

Yes, if you can see the attached screenshot, UniversityLocationID is indeed a primary column.

We know it's a PK even if the attached screenshot doesn't show it (the columns were scrolled down) simple_smile But the question was is it an Identity Column? Does it get populated automatically inside the database? Which database are you using? Is it Oracle? if yes, is the field a sequence field? is it updated or populated by a trigger?

redbullchronic wrote:

as for UniversityLocation.IsNew is true? I did not set that property, but at the point I need to make a intermediate entity, my UniversityLocationEntity has already been saved to the database successfully.

If the UniversityLocation has already been saved successfully then the IsNew should be false. Would you please check the database to see if a record is inserted in this case (which confirms a successful save operation)?