What is the best way to copy record in the same table ?
Something like that:
Address address = AddressDAO.get(id);
address.setId(null);
AddressDAO.add(address);
Yes, that should work.
I'm not sure whether hibernate doesn't check object references, so if this doesn't work you might need to create a new instance and copy all the properties (using BeanUtils.copyProperties
, or even BeanUtils.cloneBean(..)
), and then set the ID to null/0.
It would work but it's best if you express your intent (cloning) in your domain mode. Setting a field to null is just an implementation detail and carries no meaning.
Address address = AddressDAO.get(id);
Address clone = address.cloneMe();
AddressDAO.add(clone);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With