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)

Problem Description:

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

Solution – 1

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)

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject