Hope you used @PrePersist. I think that is best way.
It would be a help to others who are looking the answers for the same problem if you post your method you came across than just saying "Problem solved". And other hand there maybe better method than you used. So others may help you in that case.