ORM relationships

This question is more of an CF9 ORM thing but as I am using CB to
achieved this I assume its the appropriate place to ask.

my understanding of ORM.

I created 2 persistent components. Company and Campaign. the
relationship is easy 1 company has many campaigns they run. so my ORM
model is like so...

company.cfc

Are you calling this way:

id = campaign.getCompany().getCom_ID() ?

Hi Aaron,

I see from dumping the rc scope its an array and thats makes sense.
that does work now yes.

thanks

when i save the FK also does not update does that have to be updated
different?

  /* save campaign */
  void function saveCampaign(event) output=false{
    var rc = event.getCollection();

    if( rc.camp_id neq 0 ){
      rc.ocampaignbean = campaignService.get(rc.camp_id);
    }
    else if (rc.camp_id eq 0){
      rc.ocampaignbean = campaignService.new();
    }
    populateModel(rc.ocampaignbean);
    campaignService.save(rc.ocampaignbean);

    flash.put("message","Task saved sucessfully!");

    setNextEvent('dashboard/index');
  }

Aldo note the orm service get method has a convention that if the Id
is 0 it gives back a new entity or you. This can save some time also

what about when saving does 0 insert? this insert fails when
rc.camp_id is eq to 0 ? update works ...

  /* save campaign */
  void function saveCampaign(event) output=false{
    var rc = event.getCollection();

    if( rc.camp_id neq 0 ){
      rc.ocampaignbean = campaignService.get(rc.camp_id);
    }
    else if (rc.camp_id eq 0){
      rc.ocampaignbean = campaignService.new();
    }
    populateModel(rc.ocampaignbean);
    campaignService.save(rc.ocampaignbean);

    flash.put("message","Task saved sucessfully!");

    setNextEvent('dashboard/index');
  }

org.hibernate.StaleStateException: Batch update returned unexpected
row count from update [0]; actual row count: 0; expected: 1

confused!
Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

I have that effect on people :slight_smile:

it seems that hibernate is trying to update when it should be
inserting. for example I create new campaign URL

/index.cfm/dashboard/editorCampaign/camp_id/0

camp_id is 0

this bit now runs in my handler...

else if (rc.camp_id eq 0){
   rc.ocampaignbean = campaignService.new();
       }
populateModel(rc.ocampaignbean);
campaignService.save(rc.ocampaignbean);

then I get the error above on save. if i take out the populateModel it
works but no values get populated and i get an empty insert.

Make sure nothing weird is populating into the entity, if you populate the ID with 0 then you will get an error.

The populate methods have two arguments: include, exclude, make sure you use them to exclude or include pieces like the ID. That is most likely your error, you are populating the NEW detached entity with a 0 ID and then trying to persist it.

Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

O I see! Thanks makes sense. Thanks