Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

(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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 🙁

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading