The problem is the @Id
generator strategy you’re using there. I suppose you are using an automatic identifier strategy like IDENTITY or SEQUENCE.
Because you are copying the id
as well, Hibernate will think that you want to persist a new entity, but that entity already has an identifier.
Just make sure you skip the id
in the cloned entity and leave it null
. This way, Hibernate will see that you passed a transient entity and will persist it for you.
Since this question is a very good one, I also wrote an article about the best way to clone or duplicate an entity with JPA and Hibernate.