5 Replies Latest reply on May 30, 2011 8:21 PM by scope_talka

    Hibernate Search 2

    scope_talka Newbie

      Can someone help with the following problem(Hibernate Search)? I keep getting bunch of errors every time I run the app. Here is the class.


      -------------------------------------------------------------------------------------------------------------------------------------------



      @Name( "userDao" )
      @Scope( ScopeType.STATELESS )
      @AutoCreate
      public class UserDAO {
              
              private final EntityManager em;
              @In FullTextEntityManager fem;
              
              public UserDAO() {
                      em = ( EntityManager )Component.getInstance( "entityManager", true );
              }
              
              @SuppressWarnings( "unchecked" )
              public List<User> searchUsers( final String text ) {
                      final Query query;
                      
                      try {
                              FullTextEntityManager fem = Search.getFullTextEntityManager( em );
                  query = fem.createFullTextQuery( getLuceneQuery( text ), User.class );
                  return query.getResultList();
              } catch ( ParseException ex ) {
                  log.error( "search error", ex );
                  return null;
              }
              }
              
              private org.apache.lucene.search.Query getLuceneQuery( final String queryString ) throws ParseException {
                      try {
                          Map<String, Float> boostPerField = new HashMap<String, Float>( 2 );
                          boostPerField.put( "subject", (float) 4 );
                          boostPerField.put( "description", (float) 1 );
                          String[] searchFields = { "subject", "description" };
                          QueryParser parser = new MultiFieldQueryParser( Version.LUCENE_31, searchFields, new StandardAnalyzer( Version.LUCENE_31 ), boostPerField );
                          parser.setDefaultOperator( QueryParser.Operator.OR );
                          org.apache.lucene.search.Query luceneQuery = null;
                          luceneQuery = parser.parse( queryString );
                          return luceneQuery;
                      } catch ( ParseException ex ) {
                      return null;
                  }
         }    
      }




      -------------------------------------------------------------------------------------------------------------------------------------------
      Basially, injecting the FullTextEntityManager isn't working, I need the method(searchUsers) to return list of user objects and when I run the app I get the following error.
      ( @In attribute requires non-null value: userDao.fem )


      someone please help!!!

        • 1. Re: Hibernate Search 2
          Tim Evers Master

          This message is pretty well known if you understand the Seam framework.


          If you haven't read the seam doco you really should do so as it will explain this problem


          So, you have


          @In
          FullTextEntityManager fem
          



          So Seam is going to look in its contexts for a component called 'fem'. Which going from your error message it cannot find. As I am not familiar with your codebase that is something you are going to need to look at.


          I'm not sure why you are doing things the way your are. I really think you need to look at some of the examples that come with the Seam download.


          Here's some things I notice.


          First


          public UserDAO() {
              em = ( EntityManager )Component.getInstance( "entityManager", true );
          }
          



          Last time I checked you MUST NOT put a constructor on a Seam bean. It wont work the way you think. Seam does NOT instantiate a Seam bean the way a normal object is instantiated so get rid of it.


          Instead do this.


          @In
          private EntityManager entityManager;
          



          Second,


          If you are doing this:


          FullTextEntityManager fem = Search.getFullTextEntityManager( em );
          



          Why on earth are you trying to @In(ject) it? @In means that you want Seam to look for a bean in one of its contexts. But, when you get to that line you throw away whatever it injected anyway. Once again I think this comes down to the fact that you do not understand how Seam works and need to read the doco and study the examples before you try writing code.


          You either @In(ject) a bean or you manually instantiate an object NOT both. The general rule of thumb is.


          You can only use @In to receive an instance of a Seam bean. Where a Seam bean refers to a class that is annotated with the @Name annotation. (Now this is not 100% correct because you can use xml to do mappings which is how the entityManger is done AFAIK).


          Because a Seam bean is not just an instance of an object but rather an instance of a proxied object you CANNOT use the new operator OR have a default constructor. Using the new operator will mean that that instance is not being handled by the Seam lifecycle. And a default constructor will just never execute.


          If you want something to happen when a Seam bean is instantiated then look into the @Create annotation.


          But, to be totally honest your problems are because you have not spent enough time researching Seam. Seam is a complex framework that has a rather steep initial learning curve. This does mean that in the beginning it can be very frustrating to work with but, once you have got through that initial pain it is a rather useful framework with lots of nice features.

          • 2. Re: Hibernate Search 2
            scope_talka Newbie

            Thanks for replying Tim. I guess I really need to look at the doc again. Would it be possible to get a code sample on how to implement hibernate-search in seam by any chance? I already checked the seam file but could not find sample codes with hibernate-search. Thanks.
            Sorry about the multiple thread issue.

            • 3. Re: Hibernate Search 2
              Tim Evers Master

              An amazing thing you can do on the internet


              That link would lead you here. (second link I think)


              Once again. Reading the doco would be the absolute best thing you could do right now.

              • 4. Re: Hibernate Search 2
                Tim Evers Master

                By the way. The very last line of that doco page is


                Check the DVDStore or the blog examples of the JBoss Seam distribution for a concrete use of Hibernate Search.
                



                Seems to me you need to take a more careful look at the examples.

                • 5. Re: Hibernate Search 2
                  scope_talka Newbie

                  Really appreciate the help. I will do just that right away. Thanks.