Hello,
probably the most efficient way to define a unique contraint is to delegate this sort of checks to the database, using
Code:
@UniqueConstraint
or
Code:
@Column(unique=true)
In this way you don't need to make a secondary query the see if other entities exist, and DB integrity is ensured.
This doesn't solve your problem if you want to give immediate feedback to user though; if you are using this in a seam form to immediately inform to users the username is not available it wouldn't work;
in that case you probably want an entity validator which hits the database to check, and customize the message; I don't know how to bind the message to the correct field though.
To bind the value to the correct field I would define a validator bound to the single field, and use it only for the username of this entity: the validator would receive the proposed value and you may check on DB to see if it is available (you don't need the entity for this, and you know the class type). So you are not making a reusable validator, but it keeps things simple.
Be aware that the latter solution generates a database hit each time a user types something in the field.. you may want to avoid this type of ajax validation when not strictly required by your specs.
regards,
Sanne