Ch’ti JUG




                           Hibernate Search
                           et Bean Validation

                 ...
Ch’ti JUG                   Emmanuel Bernard
             Hibernate Search in Action
             blog.emmanuelbernard.c...
Ch’ti JUG




                                Hibernate Core


                             Emmanuel Bernard
             ...
Ch’ti JUG                               Performance
             Transactional write behind
             sophisticated s...
Ch’ti JUG                                      Flexibility
               Override any SQL
               DDL generation...
Ch’ti JUG                                              Tips
             Use conversation scoped persistence
            ...
Ch’ti JUG




              JSR-303 - Bean Validation


                             Emmanuel Bernard
                    ...
Ch’ti JUG


             Enable declarative validation in your
              applications
             Constrain Once, V...
Ch’ti JUG                                  Constraints
             Constraint
              • restriction on a bean, fie...
Ch’ti JUG   Constraints in the Java Ecosystem
             Where should they be applied

                               J...
Ch’ti JUG                  What is the solution?
             Uniform way to express a constraint
              • everybo...
Ch’ti JUG                   Declare a constraint
            public class Address {
               @NotNull
              ...
Ch’ti JUG                                         Groups
             Subset of constraints

             Partial valida...
Ch’ti JUG


            interface Billable {}

            interface BuyInOneClick extends Billable, Default {}

         ...
Ch’ti JUG       Create your own constraint
             Annotations with expressive names
             List of Constrain...
Ch’ti JUG



            @Constraint(validatedBy={
              SizeValidatorForCollections.class),
              SizeVal...
Ch’ti JUG




            public class SizeValidatorForString
              implements ConstraintValidator<Size, String> {...
Ch’ti JUG                                Composition
             Reuse constraints
             Expose meta-information...
Ch’ti JUG                   Composition
 Reuse constraints
 Expose meta-informations


 @NotNull @Size(min=5, max=5)
 @C...
Ch’ti JUG


               Integration
                - tools
                - plain SE
                - EE 6




 ...
Ch’ti JUG                               Bootstrap API
               extensible
               support multiple implemen...
Ch’ti JUG
            ValidatorFactory vf =
              Validation.buildDefaultValidatorFactory();

            Validato...
Ch’ti JUG                                       Message
             Can be externalized
             Internationalizati...
Ch’ti JUG                       Manual validation
             Get a Validator from a ValidatorFactory
              Set<...
Ch’ti JUG               Accessing the metadata
             DDL generation, tools, JavaScript
              generators
  ...
Ch’ti JUG     Accessing the metadata
 DDL generation, tools, JavaScript
  generators
 Metadata API
BeanDescriptor - vali...
Ch’ti JUG                          JSF 2 integration
             Zero conf
             Validate input components
     ...
Ch’ti JUG                      Java Persistence 2
             On entity change
              • validation
              ...
Ch’ti JUG                                     Java EE 6
             Validator as an injectable resource


             @...
Ch’ti JUG                          Bean Validation
             Status
             Todo
              • Well... nothing...
Ch’ti JUG                Hibernate Validator 4
             Bean Validation is in proposed final draft
              • RI...
Ch’ti JUG                                   Questions?
             JCP.org
               • http://jcp.org/en/jsr/detail...
Ch’ti JUG




                            Hibernate Search
               Full-text search for Hibernate Apps


          ...
Ch’ti JUG


             Understand what full-text search does for
              you
             Understand the magic s...
Ch’ti JUG                     What is searching?
             Searching is asking a question

             Different way...
What is searching?
Ch’ti JUG


 Searching is asking a question

 Different ways to answer
   • Categorize data up-front
...
Ch’ti JUG
                               Human search
                            in a relational world
             wher...
Ch’ti JUG                          Full Text Search
             Search by word
             Dedicated index
           ...
Ch’ti JUG   Some of the interesting problems
               bring the “best” document first
               recover from ...
Ch’ti JUG                        Find by relevance
             Best results first
               • very human sensitive
...
Ch’ti JUG       Extracting the quintessence
             Word: Atomic information

             Analyzer
              •...
Approximation
Ch’ti JUG


             Recover from typos and other
              approximations

             Fuzzy sea...
Ch’ti JUG


             n-gram
              • cut the word in parts of n characters
              • index each piece   ...
Ch’ti JUG                                       Demo




            Copyright © Emmanuel Bernard – License Creative Commo...
Ch’ti JUG                           Phonetic search
             Is it “jiroscop” or “gyroscope”
               • not so ...
Ch’ti JUG                                                    Synonyms
             Based on a synonym dictionary
        ...
Ch’ti JUG       Words from the same family
             love, lover, loved, loving
             Brutal force
           ...
Ch’ti JUG                           What’s the catch
               Lucene is quite low level
               Integration...
Ch’ti JUG
              Integration into a Java SE / EE
                            app
             Hibernate Search bri...
Ch’ti JUG         More on Hibernate Search
             Asynchronous clustering (JMS, JGroups)
             Projection
 ...
Ch’ti JUG                Asynchronous cluster
             Search local / change sent to master
             Asynchronou...
Ch’ti JUG                                        Summary
             Search for humans
             Full text tackles t...
Ch’ti JUG                                   Questions?

             http://search.hibernate.org
             http://luc...
Ch’ti JUG                                          Licence
             Les photos et logos appartiennent à leurs
       ...
Ch’ti JUG                                           Buffet


             Merci pour votre attention




             Me...
Upcoming SlideShare
Loading in …5
×

Hibernate Search & Bean Validation

4,040 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,040
On SlideShare
0
From Embeds
0
Number of Embeds
205
Actions
Shares
0
Downloads
75
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Hibernate Search & Bean Validation

  1. 1. Ch’ti JUG Hibernate Search et Bean Validation Emmanuel Bernard 21 septembre 2009 Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  2. 2. Ch’ti JUG Emmanuel Bernard  Hibernate Search in Action  blog.emmanuelbernard.com  twitter.com/emmanuelbernard  lescastcodeurs.com Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  3. 3. Ch’ti JUG Hibernate Core Emmanuel Bernard JBoss by Red Hat Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  4. 4. Ch’ti JUG Performance  Transactional write behind  sophisticated snapshot comparison  load associated objects (join, subselect, batch)  batch changes / bulk operations  lazy collections / objects  prepared statements  pre-compiled queries  smart merge  ... Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  5. 5. Ch’ti JUG Flexibility  Override any SQL  DDL generation optional  stored procedures  statistics  used named queries  stateless session  Bulk HQL  Database portability Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  6. 6. Ch’ti JUG Tips  Use conversation scoped persistence contexts aka first level cache  do not 2nd level cache for everything • reference data  set everything as lazy and use queries  Stop maintaining your home-grown ORM Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  7. 7. Ch’ti JUG JSR-303 - Bean Validation Emmanuel Bernard JBoss by Red Hat Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  8. 8. Ch’ti JUG  Enable declarative validation in your applications  Constrain Once, Validate Anywhere Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  9. 9. Ch’ti JUG Constraints  Constraint • restriction on a bean, field or property • not null, between 10 and 45, valid email...  How is that useful • give feedback to the user • ensure that a service will behave correctly • define service range of usability • avoid adding crap to the database • unless you like fixing the data manually Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  10. 10. Ch’ti JUG Constraints in the Java Ecosystem  Where should they be applied Java Data Client Presentation Business Access Database Side Layer Layer Layer  How many model do I have? • 1 Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  11. 11. Ch’ti JUG What is the solution?  Uniform way to express a constraint • everybody speaks the same language • based on the domain model (JavaBeans™)  Standard way to validate constraints • one runtime engine • same validation implementations shared  Bridge for constraints out of Java™ land • API to access the constraint repository Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  12. 12. Ch’ti JUG Declare a constraint public class Address { @NotNull @Size(max=30, message="longer than {max} characters") private String street1; ... @NotNull @Valid private Country country; } public class Country { @NotNull @Size(max=30) private String name; ... } Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  13. 13. Ch’ti JUG Groups  Subset of constraints  Partial validation • screen of a wizard UI  Constraints applied in a given use case  Order constraint validations • which depends on other validations • when a constraint is resource/time intensive Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  14. 14. Ch’ti JUG interface Billable {} interface BuyInOneClick extends Billable, Default {} class User { @NotNull(groups=BuyInOneClick.class) PaymentMethod getDefaultCreditCard() {...} @NotNull //Default group String getUserName() {...} } @GroupSequence(sequence={Default.class, Heavy.class}) interface Complete {} Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  15. 15. Ch’ti JUG Create your own constraint  Annotations with expressive names  List of ConstraintValidators  Constraint composition Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  16. 16. Ch’ti JUG @Constraint(validatedBy={ SizeValidatorForCollections.class), SizeValidatorForString.class } ) public @interface Size { String message() default "{constraint.size}"; Class<?>[] groups() default {}; Class<? extends ConstraintPayload> payload() default {}; //custom attributes int min() default 0; int max() default Integer.MAX_VALUE; } Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  17. 17. Ch’ti JUG public class SizeValidatorForString implements ConstraintValidator<Size, String> { public void initialize(Size annotation) {} public boolean isValid(String value, ConstraintValidatorContext context) {} } Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  18. 18. Ch’ti JUG Composition  Reuse constraints  Expose meta-informations @NotNull @Size(min=5, max=5) @Constraint(validatedBy=FrenchZipCodeValidator. public @interface FrenchZipCode { String message() default "{constraint.frenchzi Class<?>[] groups() default {}; Class<? extends ConstraintPayload>[] payload() default {}; } Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  19. 19. Ch’ti JUG Composition  Reuse constraints  Expose meta-informations @NotNull @Size(min=5, max=5) @Constraint(validatedBy=FrenchZipCodeValidator.class) public @interface FrenchZipCode { String message() default "{constraint.frenchzipcode}"; Class<?>[] groups() default {}; Class<? extends ConstraintPayload>[] payload() default {}; } Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  20. 20. Ch’ti JUG  Integration  - tools  - plain SE  - EE 6 Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  21. 21. Ch’ti JUG Bootstrap API  extensible  support multiple implementations  type-safe  can override some attributes contextually  XML configuration optional • META-INF/validation.xml Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  22. 22. Ch’ti JUG ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); ValidatorFactory vf = Validation.byDefaultProvider() .configure() .messageInterpolator( containerMI ) .traversableResolver( jpaTR ) .constraintValidatorFactory( webBeansDI ) .buildValidatorFactory(); ValidatorFactory vf = Validation .byProvider(HibernateValidationProvider.class) .configure() .messageInterpolator( containerMI ) .failFast() .enableLegacyConstraints() .buildValidatorFactory(); Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  23. 23. Ch’ti JUG Message  Can be externalized  Internationalization  Interpolate constraint parameters • must be shorter than {min}  Custom MessageInterpolator strategy • Useful for application frameworks • Contextual data • Locale Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  24. 24. Ch’ti JUG Manual validation  Get a Validator from a ValidatorFactory Set<ConstraintViolation<User>> errors = validator.validate(user); Set<ConstraintViolation<User>> errors = validator.validate(user, BuyInOneClick.class);  ConstraintViolation • error message / message template • invalid value • context Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  25. 25. Ch’ti JUG Accessing the metadata  DDL generation, tools, JavaScript generators  Metadata API BeanDescriptor - validator.getConstraintsForClass(User PropertyDescriptor - beanDescr.getConstrainedPropertie ConstraintDescriptor - descr.getConstraintDescriptors( - constrDescr.getComposingConstra  Shines with: • composition • built-in annotations Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  26. 26. Ch’ti JUG Accessing the metadata  DDL generation, tools, JavaScript generators  Metadata API BeanDescriptor - validator.getConstraintsForClass(User.class) PropertyDescriptor - beanDescr.getConstrainedProperties() ConstraintDescriptor - descr.getConstraintDescriptors() - constrDescr.getComposingConstraints()  Shines with: • composition • built-in annotations Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  27. 27. Ch’ti JUG JSF 2 integration  Zero conf  Validate input components • find property via Expression Language • call Bean Validation on input value • return localized error messages • use JSF user Locale • custom MessageInterpolator Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  28. 28. Ch’ti JUG Java Persistence 2  On entity change • validation • can select the groups validated  Make use of a custom TraversableResolver • do not traverse associations Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  29. 29. Ch’ti JUG Java EE 6  Validator as an injectable resource @Resource Validator validator; //or @Resource ValidatorFactory vf; Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  30. 30. Ch’ti JUG Bean Validation  Status  Todo • Well... nothing  Give us feedback! Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  31. 31. Ch’ti JUG Hibernate Validator 4  Bean Validation is in proposed final draft • RI available  Road Map • working on the TCK • backward compatible with legacy Hibernate Validator usage • some cool ideas out of the spec scope  License • ASL 2.0 Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  32. 32. Ch’ti JUG Questions?  JCP.org • http://jcp.org/en/jsr/detail?id=303 • http://people.redhat.com/~ebernard/ validation  http://in.relation.to • Search for ‘Bean Validation’  Hibernate Validator • http://validator.hibernate.org  http://forum.hibernate.org/ viewforum.php?f=26 Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  33. 33. Ch’ti JUG Hibernate Search Full-text search for Hibernate Apps Emmanuel Bernard JBoss by Red Hat Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  34. 34. Ch’ti JUG  Understand what full-text search does for you  Understand the magic sauce: analyzers  Full-text search and applications: how does it fit?  Bring the Wow! effect to existing applications Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  35. 35. Ch’ti JUG What is searching?  Searching is asking a question  Different ways to answer • Categorize data up-front • Offer a detailed search screen • Offer a simple search box Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  36. 36. What is searching? Ch’ti JUG  Searching is asking a question  Different ways to answer • Categorize data up-front • Offer a detailed search screen • Offer a simple search box Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  37. 37. Ch’ti JUG Human search in a relational world  where? • which columns, which tables  column != word • wildcard queries?  did you say “car” or “vehicle”?  cympausium or simposyum?  Order results by relevance  How to do that in SQL? Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  38. 38. Ch’ti JUG Full Text Search  Search by word  Dedicated index • inverted indices (word frequency, position)  Very efficient  Full text products: • embedded in the database engine • black box / appliance • library embeddable like Lucene Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  39. 39. Ch’ti JUG Some of the interesting problems  bring the “best” document first  recover from typos  recover from faulty orthography  find from words with the same meaning  find words from the same family  find an exact phrase  find similar documents Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  40. 40. Ch’ti JUG Find by relevance  Best results first • very human sensitive  Prioritize some fields over others  The more matches, the better • for a given key word per document • for a given document the amount of matching key words  Similarity algorithm Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  41. 41. Ch’ti JUG Extracting the quintessence  Word: Atomic information  Analyzer • Chunk / tokenize the text into individual words • Apply filters • remove common words • lower case  One tokenizer  Some filters Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  42. 42. Approximation Ch’ti JUG  Recover from typos and other approximations  Fuzzy search • query time operation • Levenshtein distance (edit distance) Hibernate Hibrenate Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  43. 43. Ch’ti JUG  n-gram • cut the word in parts of n characters • index each piece hibernate Index hib ibe ber ern nat ate Query ybe ber ern nat ybernat  Indexing + query time strategy • use a TokenFilter Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  44. 44. Ch’ti JUG Demo Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  45. 45. Ch’ti JUG Phonetic search  Is it “jiroscop” or “gyroscope” • not so useful in daily life  Several phonetic algorithms • Soundex • Metaphone (JRSKP) • mostly for latin languages  index the phonetic equivalent of a word  Indexing + query time strategy • use a TokenFilter Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  46. 46. Ch’ti JUG Synonyms  Based on a synonym dictionary  index a reference word in the index like cherish car banger love auto jalopy I like to drive my auto around I love to drive my banger around I love to drive my auto around I cherish to drive my car around  Indexing + query time strategy • use a TokenFilter Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  47. 47. Ch’ti JUG Words from the same family  love, lover, loved, loving  Brutal force • index all variations of a word  Stemming • Porter algorithm for English • Snowball Stemmer for most Indo-European languages  Indexing + query time strategy • use a TokenFilter Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  48. 48. Ch’ti JUG What’s the catch  Lucene is quite low level  Integration into an application model  Index synchronization  Object model conversion  Programmatic mismatch Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  49. 49. Ch’ti JUG Integration into a Java SE / EE app  Hibernate Search bridges • Hibernate Core and Java Persistence • JBoss Cache & Infinispan (More to come) • Apache Lucene  Transparent index synchronization • event based  Metadata driven conversion • annotation based  Unified programmatic model • API • semantic Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  50. 50. Ch’ti JUG More on Hibernate Search  Asynchronous clustering (JMS, JGroups)  Projection  Filters  Index sharding  Custom DirectoryProvider (eg. JBoss Cache, Infinispan based)  JBoss Cache is full text searchable  Native Lucene access Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  51. 51. Ch’ti JUG Asynchronous cluster  Search local / change sent to master  Asynchronous indexing (delay)  No front end extra cost / good scalability Slave Hibernate Database Lucene + Directory Search request Hibernate Search (Index) Copy Index update order Copy Hibernate Master Lucene + Directory Index update JMS Process Hibernate Search (Index) queue Master Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  52. 52. Ch’ti JUG Summary  Search for humans  Full text tackles those problems • relevance • (human) fault tolerance • stemming and synonyms • incremental search  Barrier of entry has lowered: Go for it! • POJO based approach • infrastructural code tackled by frameworks • unified programmatic model 49 Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  53. 53. Ch’ti JUG Questions?  http://search.hibernate.org  http://lucene.apache.org  Hibernate Search in Action • Manning  http://in.relation.to  http://blog.emmanuelbernard.com Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  54. 54. Ch’ti JUG Licence  Les photos et logos appartiennent à leurs auteurs respectifs  Le contenu de la présentation est sous licence Creative Commons 2.0 France • Contrat Paternité • Pas d'Utilisation Commerciale • Pas de Modification  http://creativecommons.org/licenses/by-nc-nd/2.0/fr/ Copyright © Emmanuel Bernard – License Creative Commons 2.0 France
  55. 55. Ch’ti JUG Buffet  Merci pour votre attention  Merci à Norsys pour son sponsoring Copyright © Emmanuel Bernard – License Creative Commons 2.0 France

×