Can not insert a record using ORM only update

I have come back to visit this. I know I have asked this question but
still I cannot get this to work. I would like to know if anyone else
can if issues is ONLY with insert not update..

the following code should return a new (blank) entity, however it
doesn't.

                  rc.camp_id = 0;
    // get entity, 0 as id means it will give you a new entity
    campaign = campaignService.get(rc.camp_id);

    // populate and EXCLUDE the id, you don't need it
    populateModel(model=campaign, excludes="camp_id");

the error is "Variable CAMPAIGN is undefined". this suggests to me
that campaign is not created with a new entity if i acuallty put a id
in the rc.camp_id then yes campaign gets created and record get
updated.

so insert does not work, I have also tried to just create a new
entity like so and replaced campaignService.get...

campaignService.new(); but this gives me the error....

"Error populating bean model.campaign with argument CAMP_ID of type
class java.lang.String."
"Simple values are booleans, numbers, strings, and date-time values."

now I have check and check CAMP_ID is 0!

and now the error is on populating "populateModel(model=campaign,
excludes="camp_id")"

I have tried removing camp_id hard coding camp_id to 0 and more.

is this still just me or a bug?

I can create a new entity

Thanks

Glyn

Glyn,

You can try

populateModel(model=campaign, excludes="rc.camp_id");

Or

if (rc.camp_id EQ 0) {event.removeValue("camp_id");}

populateModel(model=campaign);

If you are running the latest BaseORMService, 0 or "" (empty string) will return a "new" entity. But of course, if you have the ID in the rc, then it will populate it with 0 forcing it to try an update and not an insert.

Also, make sure you have a generator set on your entity if you aren't populating the ID yourself. For example.

<cfproperty name="camp_id" type="numeric" fieldtype="id" generator="identity" />

Hope this helps,

Curt Gratz

Computer Know How

Thanks,

Yes tried both of them I am running M5 (m6 gives me errors mapping
error)

where do I find the latest BaseORMService I will try that next?

My service layer is like this. I use the virtual one, is this correct
then?

component extends="coldbox.system.orm.hibernate.VirtualEntityService"{

  public any function init(){
    super.init( "campaign", true);
      return this;
  }

  // Override or add methods as you see fit now.
}

Hi Curt,

If I replace just BaseORMService from M5 to M6 I get the error below.
revert it back and its ok again. was there a change in M6?

Application Execution Exception
Error Type: Autowire.AutowireException : [N/A]
Error Messages: Error autowiring handlers.dashboard. Error
constructing model: campaignService Could not find the ColdFusion
component or interface

Did you restart CF after replacing? Or at least reinit?

Curt

yes, I have an old M6 release that has bugs I have found out, so I
will download and try again. Thanks

Ok I have it working now. the issues seems to be in ColdBox when
setting a id to 0 it .save() does not insert but try to update. I have
to remove the id completely to get it to do an insert below works,
however if I remove the id and set it 0 I get an error. for now this
will do. but would be good to know why this is the case.

    void function saveCampaign(event) output=false{
    var rc = event.getCollection();

    if (rc.camp_id EQ 0) {event.removeValue("camp_id");}

      if(rc.camp_id neq "" ){
        campaignEnity = campaignService.get(rc.camp_id);

      }
      else{
        campaignEnity = campaignService.new("campaign");
        event.removeValue("camp_id");

        }

        populateModel(model=campaignEnity);
        campaignService.save(campaignEnity);

        rc.ocampaignbean = campaignEnity;

        event.setView("campaign/edit");
      }

I told you, its the population, if you override the ID to 0 then hibernate thinks the ID is 0 and tries to persist it that way. That’s why if you create a single method for insert/update, you need to be careful how you populate your entity.

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

Sorry Luis, yes you have (twice). I misunderstood you at first as I
read "make sure campaign_id = 0 if blank" but then you said exclude it
from the population (excludes="campaign_id") which makes sense now.

Sometimes I need to be told twice, sorry :frowning: there were other issues
that threw me on this and confused the situation. like having an out
of date base orm file and stuff that also made me get my wires
crossed. M6 version I downloaded works well now, thanks

well, now thats drummed into me, on with my learning ORM.

Thanks Luis and Curt.

I think Curt knows I can be a pain I used him via your alliance
partner network sometime ago, he was fantastic, more so for putting up
with my stupid questions lol.

:0)

JAJAJ, no problems man!! We are all here to learn from each other, this is great!!

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