Entity Clone how do it

I know that could be a stupid question, but I didn’t find nothing about this, to understand how do it and understand if there is a fast mode.
I am writing a function into service bean, it receive an id parameter of column a table, using EntityManager I load an instance of entiy using find method. Now I need to do a clone this entity instance, change few fields on new instance, and insert new row into table database. Column id of table is managed like integer auto incremental.

I know that I can create a new object and then assign all fields variable manually, but I’d like use a dynamic method, without maintenance function copy each time that add a new field.

thanks

Hi,

currently there is no possibility to transparently clone an entity (neither through the UI nor through an API). A similar question is here: Cuba Platform - Copy Action - Stack Overflow

However, it seems to not to be a super big problem (programming wise). But there are some gotchas. One thing is that what happens about soft deleted references or what about a composition to another entity.

So it seems it should be doable of creating such a app component.

Bye
Mario

1 Like

There are copy() and deepCopy() methods in the MetadataTools bean. They copy the state by traversing all entity attributes known to metadata.

2 Likes

Using the deepCopy() leaves you with all the same UUID as seen here: topic. This might become very erroneous if there are relations depending on them.