-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: How to index a View ?
PostPosted: Mon Dec 31, 2012 12:07 am 
Newbie

Joined: Mon Dec 24, 2012 6:32 am
Posts: 17
Hi All,
I am using JPA and Hibernate Search.I have a non-materialised view which is mapped to an entity. I have indexed the fields however it is not indexing the entity even if I use manual indexing using Mass-Indexer. I read in Hibernate Search in action that as there are no CRUD operations Hibernate Events are not fired and hence it does not perform automatic indexing. Hence manual indexing is required. One solution I thought of is to write custom events which are fired when any CRUD is performed on the tables which compose the view. But I am not sure exactly how to implement this. Or are there any better options.

The indexing has be immediate, i.e immediately after insertion the record should be searchable.
The ER structure is Customer Detail is the base class which contains all the address details like country,state etc.Private Customer is a sub-class of Customer Details and contains details like first name ,last name ,mobile etc. And finally Customer Identification contains details like Passport details,driving licence details etc. It has a one to many relationship with Customer Detail table with one customer having many identity proofs.
Thanks a lot in advance ,

Info for Code: Following is the code for the entity mapped to the view. It's id is in the base entity. The ID is a composite key, for which I have written a field bridge.

Code:
@Entity
@Table(name = "CUSTOMER_SEARCH_V")
@Indexed
@NamedQueries({
      @NamedQuery(name = "CustomerSearch.findAll", query = "SELECT c FROM CustomerSearch c"),
      @NamedQuery(name = "CustomerSearch.findByCriteria", query = "SELECT c FROM CustomerSearch c WHERE UPPER(c.firstName) LIKE ?1 OR c.mobile LIKE ?1 OR c.customerUniqueNo LIKE ?1 OR c.idfNo LIKE ?1") })
public class CustomerSearch extends BaseEntity implements Serializable {
   private static final long serialVersionUID = 1L;
   public static final String FIND_BY_CRITERIA = "CustomerSearch.findByCriteria";

   @Column(name = "CUSTOMER_UNIQUE_NO")
   private String customerUniqueNo;

   private String address1;

   @Column(name = "FIRST_NAME")
   @Field
   private String firstName;

   @Column(name = "LAST_NAME")
   @Field
   private String lastName;

        @Column(name="MOBILE")
        @Field
   private String mobile;
....
(remaining part are getter setters etc)

_________________
Sourabh Ghorpade


Top
 Profile  
 
 Post subject: Re: How to index a View ?
PostPosted: Thu Jan 03, 2013 10:09 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Do you actually write to this view using Hibernate, or is the problem that you're writing to a different table which affects the content of this view?

Guessing it's the second case, yes I agree you could use a custom event listener; you would need to listen to changes of the entity which is being written (the changes which need to affect CustomerSearch) and trigger indexing updates of the related CustomerSearch instances.

I'd suggest to checkout the Hibernate Search sources and have a look for org.hibernate.search.event.impl.FullTextIndexEventListener in the hibernate-search-orm module.
The methods like onPostDelete and onPostInsert, onPostUpdate do something very similar to what you need;
your code would be much simpler as it doesn't need to be "general purpose" and can take advantage of the specific details of your model.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: How to index a View ?
PostPosted: Fri Jan 04, 2013 12:43 am 
Newbie

Joined: Mon Dec 24, 2012 6:32 am
Posts: 17
Hi Sanne,
Thanks for the reply. Yes it is the second case and I can take advantage of the specific details of my model. I will have a look at FullTextIndexEventListener and its events. Can you explain exactly how to override/write custom events for tables ?
Thanks a lot ,

_________________
Sourabh Ghorpade


Top
 Profile  
 
 Post subject: Re: How to index a View ?
PostPosted: Fri Jan 04, 2013 6:47 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
you can't listen to events changing a "table" specifically, but you can listen to Hibernate events on Entities.

This is the general documentation:
http://docs.jboss.org/hibernate/orm/4.0/hem/en-US/html/listeners.html

The FullTextIndexEventListener is special as the core Hibernate ORM looks for this class specifically to auto-register it without need of configuration; your custom listener will need some configuration as described in the above link.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: How to index a View ?
PostPosted: Fri Jan 04, 2013 11:44 pm 
Newbie

Joined: Mon Dec 24, 2012 6:32 am
Posts: 17
Thanks Sanne,
I was just looking at the same thing.I will try that and let you know how it goes,
Thanks again

_________________
Sourabh Ghorpade


Top
 Profile  
 
 Post subject: Re: How to index a View ?
PostPosted: Mon Jan 07, 2013 2:21 am 
Newbie

Joined: Mon Dec 24, 2012 6:32 am
Posts: 17
Hi Sanne,
I am facing a issue while manually indexing the View. The issue is that the data is not getting indexed.

Essentially what I am trying is that After persisting any entity which comprises the view, I call the indexing function and pass the instance of the entity to it.(I am not doing indexing after updation/deletion or events for now). Then I query the database and get an instance of the View Entity (CustomerSearch). I pass that to the index function. customerIdentityDetail and customerSearch are getting set properly, so its not a database/query issue or a issue where the entityManager has not commited the data to DB. (before calling this method I have done entityManager.flush()
so the data is getting pushed to DB before this function call)
Code:
public void startIndexer(List<CustomerIdentityDetail> customerIdentityDetails) {
      FullTextEntityManager fullTextEntityManager = Search
            .getFullTextEntityManager(entityManager);
      for (CustomerIdentityDetail customerIdentityDetail : customerIdentityDetails) {
         Long id = Long.valueOf(customerIdentityDetail.getCompositePK().getId());
         Query query = entityManager
               .createNamedQuery(CustomerSearch.FIND_BY_ID);
         query.setParameter(1, id);
         List<CustomerSearch> customerSearchDetails = query
               .getResultList();
         for (CustomerSearch customerSearch : customerSearchDetails) {
         fullTextEntityManager.index(customerSearch);
         }
      }
      //fullTextEntityManager.getTransaction().commit();
   }

I tried using commit which I had seen on a website but I have a JTA Entity manager so I can't do getTransaction().

Thanks in advance,

_________________
Sourabh Ghorpade


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.