This content has been marked as final. Show 9 replies
call the validation & throw a message when you click the create button or disable that button decoratively once you reached the count.
No. You didn't get it. The user will enter the details, and when he clicks on SAVE button, the validation should get fired to check the number of employees for that department. If the entered employee is the 6th employee for that particular department, then it should display an error message saying that there cannot be more than 5 employees.
You can create a custom method in managed bean. And on the action of the Save button, call that method and check the row count for particular department.
Also you can disable the save button from this method once the row count is reached its value.
why would you want to show a message after user input data for the 6th row? It should be before even that.
Yes, even in your case, the scenario will work, better way to check before creating.
So do I have to create a method in AM which I will be calling from UI, or there is any other way? I mean the way we create entity level or attribute level validations...
have a method in the am impl.
Why not add a businessvalidation in the EO of the Department as follows
Operation - count
Accessor - <your ChildEntity Employees>
Attribute - <Key of the child Entity>
Operator - LessOrEqualTo
CompareWith - Literal Value
Sorry to rain on the parade here, but I don't believe that any of these solutions will work correctly, at least not if your application has more than one user.
There is one way (short of using explicit locking, which is a bad idea for a web app) that I know of to implement such a requirement - it would involve creating a refresh-on-commit materialised view in the database and placing a check constraint on the MV. The MV would simply be something like "select department_no, count(*) num_employees from employees group by department_no" and the check constraint would ensure that num_employees must be less than or equal to 5.
I have built the sample application. Here it is:
By the way, here I have considered the restriction of 10 employees instead of 5.