Home > front end >  how to validate if there's already a field with the same registered number (Logical problem)
how to validate if there's already a field with the same registered number (Logical problem)

Time:12-07

(https://i.stack.imgur.com/52C98.png)

I can't let the user enter a code that already exists in the database,so i need to make one more validation for this field "code".

obviously I have to use a 'if statement' but i just can't resolve this logical problem. can someone help ?

i tried something like that:

  if(cfop.getCode().equals(code){
       throw new RuntimeException("This code already exist");
  }

but doesn't work.

i'm very beginner,so my logical is really poor :(

CodePudding user response:

If you are using JPA/JDBC, you can simply annotate this Cfop.code as unique in your Cfop.class. For example:

@Column(name="code", unique = true)
private Long/String code;

After that, you will not able to save any entity with non-unique code, it will generate error, that you need to catch with try/catch block in your method.


Feel free to ask if any of this information is not clear)

  • Related