-->
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.  [ 7 posts ] 
Author Message
 Post subject: HS: Howto have multiple class in one index(without OneToOne)
PostPosted: Fri Feb 13, 2009 7:00 am 
Newbie

Joined: Fri Feb 13, 2009 6:17 am
Posts: 5
Hi!

We want to have data from two different classes merged in one index. The classes represent tables with a one-to-one relationship but they are not marked @OneToOne in Hibernate. One class (Vehicle) is the master, the other (VehiclePrices) is initialized with the same id.

We cannot have a OneToOne because both classes live in different modules and must not know each other (only a third module knows both).

Now, we want to have the data of both classes merged in one index.

Is this possible with HS? If yes, how can we do this? If no, is there another way?

Cheers,
Chris

PS: here's some example code. This is what I tried: i put both classes in the same index - but now there is a document for Vehicle and one for VehiclePrice.

Code:
@Entity
@Indexed(index="vehicle")
public class Vehicle
{
   @Id
   @DocumentId
   @GeneratedValue
   private Long id;

   @Column(length = 17)
   @Field
   private String vin;
...
}


Code:
@Entity
@Indexed(index = "vehicle")
public class VehiclePrice
{
   @Id
   @DocumentId
   private Long id;

   @Column(precision = 9, scale = 2)
   @Field
   private BigDecimal price;

   public VehiclePrice(Long vehicleId, BigDecimal price)
   {
      super();
      this.id = vehicleId;
      this.price = price;
   }
...
}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 14, 2009 7:43 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
This is what I tried: i put both classes in the same index - but now there is a document for Vehicle and one for VehiclePrice.

yes this should work, could you elaborate what the problem is?

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 4:05 am 
Newbie

Joined: Fri Feb 13, 2009 6:17 am
Posts: 5
s.grinovero wrote:
yes this should work, could you elaborate what the problem is?


The "problem" is that the given code results in two different documents (for the same id). I want to have the fields of both classes merged into one document!

--
Chris


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 5:33 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Ah, now I understand, thanks.
Which entity do you expect to be returned when doing a Query? you probably want to index them both in two different indexes, having a "master" entity and a secondary one just to add some fields, or index only one of them.

When using a @ClassBridge on the class you can customize all fields getting added to the index for that class; you may want to implement a ClassBridge which is capable of loading the other entity and add the fields you want.

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 5:50 am 
Newbie

Joined: Fri Feb 13, 2009 6:17 am
Posts: 5
s.grinovero wrote:
Which entity do you expect to be returned when doing a Query?

I use projections and a ResultTransformer - I just need the indexing.

s.grinovero wrote:
When using a @ClassBridge on the class you can customize all fields getting added to the index for that class; you may want to implement a ClassBridge which is capable of loading the other entity and add the fields you want.


I think using the @ClassBridge with loading entities will be too slow (n+1 problem) - we have a lot of vehicles in the database + 7 entities in other modules for each vehicle.

I am now experimenting with using Lucene directly and creating the index manually. What I try to do is just update only the fields of the document that are in the current entity (e.g. price in VehiclePrice). This seems to work using "raw" Lucene.

Now, I wanted to use Lucene via HS - but I cannot find a way to access IndexWriter. I also tried to get some LuceneWork into the queue. Neither of this seems to work. Have you got a clue?

Thanks,
Chris


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 6:05 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
using the @ClassBridge with loading entities will be too slow (n+1 problem) - we have a lot of vehicles in the database + 7 entities in other modules for each vehicle

You can load them all in one single query, so you can avoid the n+1 problem. Also you can fetch join whatever you need additionally, and caches are going to help you anyway.

Quote:
What I try to do is just update only the fields of the document that are in the current entity (e.g. price in VehiclePrice). This seems to work using "raw" Lucene

Interesting; as updating documents is not supported in "raw Lucene" what are you doing? putting in "store" all fields to rebuild the data later? you might be interested to know that usually it's faster to query the database than to extract existing information from you index. It's also safer to use the DB for consistency reasons.

Quote:
I also tried to get some LuceneWork into the queue.

Obviously this wasn't meant to be done, IMHO you're the first trying to do this. What are you doing? some code?

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 8:46 am 
Newbie

Joined: Fri Feb 13, 2009 6:17 am
Posts: 5
s.grinovero wrote:
You can load them all in one single query, so you can avoid the n+1 problem. Also you can fetch join whatever you need additionally, and caches are going to help you anyway.

I cannot do this because of the modularity of the system - the system has a plugin architecture, where some plugins can be added to the system at deploy time (similar to Eclipse/OSGi).

I am now doing the following now (this seems to work but is not nice):
Code:
   // VehiclePriceRepository#save(VehiclePrice vehiclePrice)
   // FullTextSession and IndexWriter are provided

   session.save(vehiclePrice);
   Query idQuery = new TermQuery(new Term("id", String.valueOf(id)));
   FullTextQuery q = fullTextSession.createFullTextQuery(idQuery, Vehicle.class);
   q.setProjection(ProjectionConstants.DOCUMENT);
   Document document = (Document) ((Object[]) q.uniqueResult())[0];
   document.add(new Field("price", vehiclePrice.getPrice().toPlainString(), Field.Store.YES,
      Field.Index.NOT_ANALYZED));
   indexWriter.deleteDocuments(idQuery);
   indexWriter.addDocument(document);


The problem now is that I have to index everything manually - otherwise HS will override the Document on saving a Vehicle instance.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.