Results 1 to 7 of 7

Thread: How to make a field unique with userdefined Exception

  1. #1
    Join Date
    Jun 2010
    Posts
    11

    Default How to make a field unique with userdefined Exception

    Hi.
    i want to make my username field as unique.
    so that if i repeats the username it will show an error like user already exists.
    can any body help me regarding this.Thanks in advance

  2. #2
    Join Date
    Dec 2007
    Location
    Stockholm, Sweden
    Posts
    190

    Default

    I was planning on making a tutorial any way, so I just started with 0, should help you I think

    http://hatimonline.com/2010/08/04/sp...ty-tutorial-0/
    Shahzada Hatim
    @geoaxis/twitter
    http://hatimonline.com

  3. #3
    Join Date
    Jun 2010
    Posts
    11

    Default

    Thanks a lot...
    But its showing an exception like..

    org.hibernate.exception.ConstraintViolationExcepti on: could not insert: [com.unique.domain.SystemUser]; nested exception is javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationExcepti on: could not insert: [com.unique.domain.SystemUser]


    means its taking the field as unique and generating the exception as and when i
    want to enter duplicate value...

    Kindly guide more....

  4. #4
    Join Date
    Dec 2007
    Location
    Stockholm, Sweden
    Posts
    190

    Default

    Have you configured the validator?
    Shahzada Hatim
    @geoaxis/twitter
    http://hatimonline.com

  5. #5
    Join Date
    Jun 2010
    Posts
    11

    Default

    My SystemUserController is like

    public class SystemUserController {

    @Autowired
    private Validator systemUserValidator;

    @InitBinder

    protected void initBinder(WebDataBinder binder) {

    binder.setValidator(systemUserValidator);

    }
    }


    and my SystemUserValidator is like

    public class SystemUserValidator extends LocalValidatorFactoryBean implements Validator {

    private static final Log logger = LogFactory.getLog(SystemUserValidator.class);

    @Override
    public boolean supports(Class<?> clazz) {
    return SystemUser.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
    super.validate(target, errors);
    SystemUser user = (SystemUser) target;
    if (user != null) {

    if( SystemUser.findSystemUsersByUsername(user.getUsern ame()).getResultList().size() > 0 )
    errors.rejectValue("username", "The username '"+user.getUsername()+"' is already in use", "The username '"+user.getUsername()+"' is already in use");
    }
    else {
    errors.reject("SystemUser object not available");
    }
    }

    suggest more

  6. #6
    Join Date
    Dec 2007
    Location
    Stockholm, Sweden
    Posts
    190

    Default

    Ah thanks for pointing out, there is an important piece missing in my blog (should have shared the code)

    It needs a @Component annotation on top for component scanning to work (the annotation is for the entire Validator class)

    Otherwise auto wired wont work.
    Shahzada Hatim
    @geoaxis/twitter
    http://hatimonline.com

  7. #7
    Join Date
    Jun 2010
    Posts
    11

    Default

    Hey it works fine now...
    Thank u very much..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •