We asked Emmanuel Bernard about how Red Hat is a leading force for open standards, and about the role the JBoss Community and JBoss Enterprise Middleware play in promoting them.
Q: Emmanuel, please tell a little bit about yourself, your job title, and what projects you are currently involved in?
A: Hi, I am a JBoss core developer, working with the Hibernate team. I co-founded and am leading Hibernate Annotations and Hibernate EntityManager (JPA implementation) as well as Hibernate Validator and Hibernate Search. These days I am working a lot in the Java Community Process (JCP). Specifically, I am a member of the JPA 2 expert group and the spec lead of JSR-303: Bean Validation.
I am also the author of Hibernate Search in Action published by Manning. You can reach me at my blog http://blog.emmanuelbernard.com or on twitter at twitter.com/emmanuelbernard
Q: What is JSR-303?
A: Bean Validation aims at standardizing constraint validation for the the Java platform. Validating data that eventually feeds into the domain model is a common task that is copied in many different layers of an application, from the presentation tier to the persistentce layer. Many times the exact same constraints and validation logic has to be duplicated in each separate validation framework, which is time-consuming and error-prone.
Bean Validation solves this problem by providing three key elements. It lets you declare constraints on your domain model using annotations like @NotNull, @Email, @OrderNumber. These constraints can be placed on a field, a getter or even at the class level itself. You can write your own custom constraint very easily. This is in fact encouraged by the specification.
The second element is the runtime validation engine. Bean Validation exposes APIs to validate object graphs and return the set of constraint violations. All invalid constraints are returned at once by the validation engine. Bean Validation also provides a metadata API that lets you query the list of constraints on a given class or property. This is tremendously useful for tools or framework used outside the Java VM runtime.
Think about a database schema generation tool or a JavaScript generator library. It would be ideal to push constraints declared on the domain model to the client and get them validated in JavaScript. The metadata API is an answer to this problem. Bean Validation is really the constraint repository and runtime engine that any other framework or application in need of validation should rely on.
Q: How does this standard benefit users? What problems will it solve?
A: By providing a uniform and standard way to express constraints in your domain model and opening standard APIs to validation or queries, Bean Validation opens the doors to reusing the same constraints and validation logic from the presentation layer to the persistence layer in a declarative way.
Traditionally, an application developer would express the constraints in the presentation layer, then validate them again at the service layer (because the service is exposed as Web Service) and finally apply them to the database or the persistence layer. This has been the root of numerous bugs and a source of frustration with the side effect of having many applications simply ignoring validations and lowering the quality of their data (and its intrinsic value).
Bean Validation has been deeply integrated with JSF 2 and JPA 2. By declaring the constraints in your domain model, validations are applied in JSF and errors reported to the user. Likewise, when JPA creates or updates en entity, validation is triggered, making sure that every entities persisted contain valid data. And this requires zero additional configuration.
I'd like to mention that even though we worked hard to make Bean Validation integrate nicely with Java EE 6, you can also use Bean Validation in an SE environment. The barriers between SE and EE are falling down one by one
Q: How will JBoss developers and customers see these benefits?
A: In a way, the JBoss community already benefits from these ideas. Hibernate Validator and its integration with Hibernate Core and JBoss Seam already offer this "declare once apply everywhere" mantra keeping data and applications at a high standard. This is something that has been present in the JBoss Enterprise Application Platform for a long time as well. The standardization effort we went through went well beyond what Hibernate Validator is capable of. You can now validate subsets of constraints. The standard also encourages third party frameworks to embrace the Bean Validation model making the model inherently more useful.
Q: How did you come to lead this effort? What have you done so far?
A: The specification has been started by Jason Carreira and I joined the expert group as lead developer of Hibernate Validator. Later, Jason could not pursue his spec lead role, and I took over the responsibility. Red Hat really wanted this specification and its benefits completed and added to the platform.
I would like to point out that Red Hat has been heavily involved in the making of Java EE 6. Here are some of the specs we have put a lot of effort into either by contributing heavily or by leading the standardization effort: Java Persistence 2, Java Server Faces 2, JSR-299 formely known as Web Beans, JSR-303 Bean Validation.
The Bean Validation specification is in proposed final draft (roughly a release candidate). We are very happy with the shape of the spec and the feedback we have received. There will be minor enhancements, bug sweeping and of course we will listen to the community feedback just like we have done in the past. We are also working on the reference implementation: Hibernate Validator 4. The implementation is not feature complete yet but fairly advanced and quite stable. Enough to give it a try.
Q: Where can I learn more about JSR-303?
A: You can go to the JCP website to download the specification PDF http://jcp.org/en/jsr/detail?id=303
You can also go to http://in.relation.to (the Hibernate team blog) where we upload the latest version of the spec regularly (the proposed final draft is there as the time we speak).
We welcome feedback, so if you would like to share your opinion, simply go to the Bean Validation forum http://forum.hibernate.org/viewforum.php?f=26
If you are interested in trying Hibernate Validator 4, check out http://validator.hibernate.org