Reputation: 14309
I have a Product
entity and table and would like the database design to allow finding a product by different keywords on top of its name, that is, like using a thesaurus e.g. product name "HDR-TD20V" should also be found by keywords "camcorder", "camera", "video camera", etc. Note that this same mechanics can be used to locate the same record from different input languages e.g. looking for "camara de video" (Spanish) or "videokamera" (German) should also find the same record.
Assuming that I am using Hibernate-search i.e. Lucene I have the following two design choices:
Product
table has a keywords
column that contain comma separated keywords for that product. This clearly violates the First Normal Form "... the value of each attribute contains only a single value from that domain.". However, this would integrate nicely with Hibernate-search.Keyword
entity table i.e. Keyword(id,keyword,languageId)
and the many-to-many association ProductKeyword(productId,keywordId)
but the integration with Hibernate-Search is not so intuitive anymore ... unless e.g. I create a materialized view i.e. select * from Product p, Keyword k, ProductKeyword pk where p.id=pk.productId and k.id=pk.keywordId
and index this materialized view.I would of course prefer the choice 2 but I am not sure how Hibernate-search would optimally cover this use-case.
Upvotes: 0
Views: 562
Reputation: 19119
Something like this should work:
@Indexed
public class Product {
@Id
private long id;
@ManyToMany
@IndexedEmbedded
Set<Keyword> keywords;
// ...
}
public class Keyword {
@Id
private long id;
// only needed if you want a bidirectional relation
@ManyToMany
@ContainedIn
Set<Product> products;
// ...
}
I am leaving out options for lazy loading etc. How exactly the JPA mapping looks like depends on the user case
Upvotes: 1