Copying an entity

more related to cf orm than coldbox.

i duplicate() an entity, set all the IDs to 0 and get this error
message when saving:

a different object with the same identifier value was already
associated with the session: [CaseBuilderBuild#24]

how can i copy all the properties and mappings of an entity and re-
persist it with a new ID?

Running duplicate() on any object is a very dangerous proposition. I would recommend you not do that.

Try adding a method to your CFC to handle the “cloning” for you. Something like so:

public void function cloneFromSource(
required any source
setFoo( arguments.source.getFoo() );
// do the same for all properties needed to be “cloned”


I use the ORMService.populate and use the excludes option.

mat... thanks for the pro tip.

i did create a 'duplicate' method in the entity that goes through all
properties/mappings, and returns a new entity.

Andrew I like your suggestion but I haven’t been able to get the syntax right. Would you happen to have an example that you could share?

There are a few different ways you can use the populate, but I have to ask one thing, when you say you can’t get it right, do you have an example of what you are doing now.

The reason is that I and others will need to see what more we can suggest. But the first thing that comes to mind, is if you are going to populate something, if it be from a form then the form will need to have the same properties as what you are trying to populate.

In other words if the entity has the property FirstName and your form has First_Name then it won’t work.

Does that help, if not let us know and give us an example so we can see and suggest.

The following works fine.

prc.applicant = populateModel( );

But who would I exclude the “bus_name” property of applicant new?


Did you read this