I followed some peoples asked similar type question already. How to generate Custom Id in JPA
But my question is one step ahead. What is best approach to generate such custom ID.
I have one table in which I want custom ID like Boy-1, Boy-2, Girl-3, Girl-4, Boy-5, Girl-6, Girl-7 as primary key.
- making many class files extending IdentifierGenerator like above link answer or
Repository
public interface YourEntityRepository extends JpaRepository<YourEntity, Serializable> {
Long countByMyId();
}
service class
public void save(MyUser user){
Long id=userRepository.countByMyId();
if(user.getGender().equals("MALE"){
user.setId("Boy-"+id);
}else{
user.setId("Girl-"+id);
}
...
Or if there is any other best way please share.