Updated hibernate function to prevent duplicates