• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Annotations: Validating two inputs.  RSS feed

 
Dilip H Pashupathi
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a situation where I need to validate the value of one variable based on the input provided in another variable. But I need to do this using Annotations. Below is the code for the same.



In the above code as you can see, "type" has two different values that is validated by pattern matcher. And the "value" variable input depends on the value of "type" variable. The validation should look something like this


And for each of the above validation a different error should be displayed to the user. Like if wrong account number, then to user message should go as "Wrong Account Number" and if payee id pattern mismatches, then error should be displayed as "Wrong Payee ID".

All of the above should happen through annotations only. I tried with the below



But its' not working. I need only one message. Problem I am facing is if Payee Id is wrong, the message is coming as invalid account number.


Please help. This is very urgent

Thanks in advance
Dilip H Pashupathi
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you specify the payee ID as the first pattern? If switching the order of patterns in the list appears to correct your problem, test what happens when account number is wrong.

I don't think that specifying patterns in a list like that is an OR thing. Rather, it seems to me like it is an AND, where the field must satisfy both/all patterns specified. See this example http://stackoverflow.com/questions/16225015/multiple-regex-patterns-for-1-field

If I'm right about that, then you'll have to think about revising your design for this class altogether or revising your validation strategy. I would vote for design because using one field value to represent two entirely different things is smelly to me.

EDIT: This Entity reflects the database design too much. This design works in a database but it does work well in an object. I think you have fallen into the trap of making your object structure mirror your DB table structure too much. The reason there's a problem called Object-Relational Impedance Mismatch is because you generally wouldn't make your objects reflect your DB structure exactly. (see more in later response)
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you *really* want to do this, you should probably create your own constraint annotation. I don't think this is the way to go though.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:create your own constraint annotation. I don't think this is the way to go though.

I agree, this is not the way to go. This way is down the way of Kludge Alley. You'd be addressing the wrong "problem" - your real problem is the value that tries to do double duty in representing two entirely different things.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I guess anything annotated as @Entity is supposed to represent a database entity. In this case I would look at shifting the validation strategy so that your annotation is not on the field but on a getter instead.

Here's what the Oracle documentation has to say about this: That is, if the persistent class uses field access, apply the Bean Validation constraint annotations on the class’s fields. If the class uses property access, apply the constraints on the getter methods.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to remember what I did in a case where there was a metadata type field like what you have. If I recall correctly, our DB design was to store the values of different types in a separate table, not in the same table as the metadata type field. In your case, there would be two separate tables joined to the table that has the "type" field: one table for account_numbers and another table for payee_ids. Yes, I think that's exactly what we did. A quick search turns up a similar example: http://stackoverflow.com/questions/18555603/joining-different-tables-based-on-column-value
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!