[coldbox-3.8.1:railo4] Problem saving ORM relationship.

Can anyone see what I am doing wrong here?

`

var layer = orm.get(“KmlLayer”,rc.id);

layer.getLegendItems().clear();

for (item in rc.legenditems){

var legendItem = orm.populate(orm.new(“KmlLayerLegendItem”),item);

layer.addLegendItem(legendItem);

legendItem.save();

}

layer.save();

`

I have an entity KmlLayer, with a one to many association KmlLayerLegendItem.

The above code is creating a new LegendItem, and saving it to a KmlLayer. The LegendItem is creating and saving to the DB fine, but the foreign key (layer_id) in the LegendItemt is not getting populated… so the LegendItem is saving to the DB, but not associated with the Layer that I add() it to.

I have a feeling it has to do with the order I am saving the layer and legendItem, but from what I can tell reading docs, what I have should be fine.

Can anyone see what I might be doing wrong?

Many Thanks in advance

Jason

ORM is a little funny like this, with which takes a bit of understanding to get your head around.

The ID will be not what you expect. Because until you save the parent of the relationship there will be no ID for the legendItem to save.

Ok maybe I should not skip over the code part.

Correct me, but layer already exists and has been persisted, then your running this is that right? I think in this case, as you’re adding them to the layer as children. I think ORM wants / needs you to save the layer and not the items.

Correct, the parent object (layer) already exists, and I am getting it with var layer = orm.get(“KmlLayer”,rc.id);… I then want to add children to it.

I’ve tried saving the parent object as well… tried all sorts of combinations but just can’t get it to save with the FK…

It’s tedious alright… I’ve got this working plenty of times before, but just doesn’t want ot play this time… Unfortunately might need to give up and go back to a good old fashion query.

Thanks Andrew

Can you share the configuration of your entities?

KmlLayer.cfc (parent entity)

`

component persistent=“true” table=“kmllayers” hint=“A KML Layer” extends=“coldbox.system.orm.hibernate.ActiveEntity”{

property name=“id” fieldtype=“id” generator=“native”;

property name=“title” ormtype=“string” length=“100”;

property name=“file” ormtype=“string” length=“200”;

property name=“isEnabled” ormtype=“boolean” default=“false” dbdefault=“false”;

//associations

property name=“group” fieldtype=“many-to-one” cfc=“KmlLayerGroup” fkcolumn=“group_id” inverse=“true” lazy=“true”;

property name=“legenditems” fieldtype=“one-to-many” type=“array” singularName=“legenditem” cascade=“delete-orphan” cfc=“KmlLayerLegendItem” fkcolumn=“layer_id” inverse=“true” lazy=“true”;

}

`

KmlLayerLegendItems.cfc (child entity)

`

component persistent=“true” table=“kmllayerlegenditems” hint=“A KML Layer Legend Item” extends=“coldbox.system.orm.hibernate.ActiveEntity”{

property name=“id” fieldtype=“id” generator=“native”;

property name=“label” ormtype=“string” length=“150”;

property name=“color” ormtype=“string” length=“16”;

property name=“dspOrder” ormtype=“float”;

}

`

Thanks Joel.

Jason

Is the code inside the loop getting run, are there any items in RC.legenditems?

Ignore that, I’ve read your post properly now!

I found the problem. If I remove ‘reverse=true’ from the association property in the parent entity, the FK saves fine with the child entity.

Given I have never been able to fully get my head around what the inverse property actually does, I don’t know why… but there it is.

Jason

Do u mean inverse?

http://www.briankotek.com/blog/index.cfm/2010/9/14/Using-ColdFusion-ORM-and-HQL-Part-3-Bidirectional-Relationships

Yes, sorry… I meant inverse

As the blog post I added shows, that attribute applies the relationship on the related entity and not the entity you put it on, hope that makes sense.