Configure Hibernate validation for bean
I need to perform validation based on the result of a SQL query.
The request is defined as annotation - like @NamedQuery in my entity bean.
According to the Hibernate documentation ( doc ), it is possible to check the bean for the following operations:
before update,
pre-insert,
pre-delete
as follows:
<hibernate-configuration>
<session-factory>
...
<event type="pre-update">
<listener class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener"/>
</event>
<event type="pre-insert">
<listener class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener"/>
</event>
<event type="pre-delete">
<listener class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener"/>
</event>
</hibernate-configuration>
The question is how to bind my bean to the validation configuration described above.
update:
entity class
...
@Entity
@NamedQuery(name = "isValutaKursExists", query = "SELECT id FROM CurrencyRate WHERE bankId = :bankNum")
@Table(name = "Currency")
public class Currency {
...
a source to share
The question is how to bind my bean to the validation configuration described above.
You will need to annotate your bean to bean annotations of the API test, to add constraints , for example @NotNull
, @Size
( built ) or define your own. But the Validation bean is not designed to perform validation based on the result of an SQL query.
By the way, you mentioned @NamedQuery
, so I think you are using Hibernate EntityManager. In this case, I would recommend integrating the Validation bean with JPA (instead of Hibernate). If you are using JPA 2.0, just put the bean implementation Validation on the classpath. If you are using JPA 1.0 see this previous answer .
a source to share
Yes, the correct approach would be a custom constraint like ValidCurrency and the corresponding ValidCurrencyValidator. You will need Hibernate session access accordingly. EntityManager in ConstraintValidator implementation. You can get some ideas on how to do this on the Hibernate wiki page - Accessing Hibernate Session in ConstraintValidator
a source to share