J.H
J.H

Reputation: 133

Hibernate: how to annotate Collection<Object> to allow search

Say I have a model A like this:

@Embeddable
class A {
    @Field
    private String name;
    @Field
    private Boolean editable;
    @IndexedEmbedded
    private B bObject;
}

where, B is, say

@Embeddable
class B {
    @Field
    private int intValue;
    @Field
    private boolean boolValue;
}

Now, I have a class C,

@Entity
class C {

    //How to annotate this to enable search like setOfA.name="searchQuery", or setOfA.bObject.intValue=5
    @ElementCollection
    private Set<A> setOfA;
}

I see that I can create custom Bridge, but can't figure out what the Bridge should do, specially to enable search like setOfA.bObject.intValue = 5.

Any help is appreciated. Thanks.

Upvotes: 3

Views: 549

Answers (3)

jpmottin
jpmottin

Reputation: 3017

Here is an working exemple:

@ElementCollection
@Field(name="listStringField")
@FieldBridge(impl=BuiltinIterableBridge.class)//ListStringBridge.class)
public List<String> getListString(){
    return listString;
}
  • @Field(name...) defines a searchable field.
  • @FieldBridge allows to my list to be searchable, but for that, you have to precise what bridge you want to use. The bridge is what will make the field indexable.
  • BuiltinIterableBridge, this bridge is natively present in packages of Hibernate Search. All you have to do, is just to use it !
  • ListStringBridge. A bridge that i have created who extends IterableBridge. You can write your own.

Upvotes: 3

Gunnar
Gunnar

Reputation: 18990

You need to add @OneToMany or @ManyToMany so it's a properly mapped association by terms of JPA. Then add @IndexedEmbedded to enable full-text search through Hibernate Search.

Upvotes: 0

Atul Kumar
Atul Kumar

Reputation: 749

use @ContainedIn over collection object like :

@ContainedIn
private Set<A> setOfA;

And use @IndexedEmbedded over other side like :

@Embeddable
class A {
    @Field
    private String name;
    @Field
    private Boolean editable;
    @IndexedEmbedded
    private B bObject;

    @OneToOne( cascade = { CascadeType.PERSIST, CascadeType.REMOVE } )
    @IndexedEmbedded
    private C cObject; 
}

Upvotes: 1

Related Questions