Using Hibernate Validation with JPA in Karaf


#1

Not sure if this is a hibernate validation, jpa or osgi issue but wonder if anyone here might have encountered this and solved it.

While looking for a solution I came across this;
https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/?v=6.0#_jpa

which says; “If you are using JPA 2 and Hibernate Validator is in the classpath, the JPA2 specification requires that Bean Validation gets enabled”.

I therefore installed the hibernate-validation feature followed by the open-jpa feature. In my JPA bundle meta file I add the validation packages under Import-Package (javax.validation, javax.validation.constraints, org.hibernate.validator).

So doesn’t this mean that Hibernate Validator is in the classpath? Shouldn’t JPA be automatically picking this without any flags in persistence.xml?

I install both hibernate validator and openjpa using the feature:install option that comes with Karaf. I did manage to get hibernate validator to work when I specifically write code to validate a class. What I am trying to achieve the JPA to automatically validate objects before persisting.


#2

I continued to investigate this and found more info here:
https://access.redhat.com/documentation/en-us/red_hat_jboss_fuse/6.2/html/apache_cxf_development_guide/validation

Where it says:

“The default mechanism for resolving a validation provider involves scanning the classpath to find the provider resource. In the case of an OSGi (Apache Karaf) environment, however, this mechanism does not work, because the validation provider (for example, the Hibernate validator) is packaged in a separate bundle and is thus not automatically available in your application classpath. In the context of OSGi, the Hibernate validator needs to be wired to your application bundle, and OSGi needs a bit of help to do this successfully.”