Ady Junior
Ady Junior

Reputation: 1080

@ContainedIn on field inside @EmbeddedId class

My code has a Place and Address classes. Address 1:N Places.

There is one index, Address Index.

When the Address is saved, that is ok, works fine. But when the Place is saved, the Index of Address isn't updated.

The problem, maybe is a bug: When @ContainedIn is on field inside @EmbeddedId class, that doesn't work. The intern mechanism isn't notified, hence index isn't changed.

I've tried some workarounds:

So, I need to make this work in some way, then my main question is... How can I make it work?

For instance: Is there some intern mechanism/class of Hibernate Search? that I could use inside of a PreInsertEventListener/PreUpdateEventListener (manually way) or somethink like that...

Code of Place class:

@Entity
public class Place {

    @IndexedEmbedded
    @EmbeddedId
    private PlaceID id;

    @ContainedIn
    @ManyToOne
    @JoinColumns({ 
        @JoinColumn(name = "address_id", insertable = false, updatable = false, referencedColumnName = "id"),
        @JoinColumn(name = "address_secondId", insertable = false, updatable = false, referencedColumnName = "secondId")
    })
    private Address address;

    @Field(store = Store.YES)
    private String name;
}

Code of Address class:

@Indexed
@Entity
public class Address {

    @FieldBridge(impl = AddressIdFieldBridge.class)
    @IndexedEmbedded
    @EmbeddedId
    private AddressID id;

    @Field(store = Store.YES)
    private String street;

    @Field(store = Store.YES)
    private String city;

    @IndexedEmbedded
    @OneToMany(mappedBy = "id.address")
    private Set<Place> places;
}

Versions of dependencies:

<hibernate.version>5.2.11.Final</hibernate.version>
<hibernate-search.version>5.8.0.Final</hibernate-search.version>
<lucene.version>5.5.4</lucene.version>

Code to update entity and index:

@Test
public void givenPlaces_updateAddressIndex(Address address) {
    List<Place> places = new ArrayList<>();

    for (int i = 0; i < 10; i++) {
        Place place = new Place(new PlaceID((long) new Random().nextInt(500), address));
        place.setName("Place " + new Random().nextInt(500));
        places.add(place);
    }

  placeRepository.save(places);
}

Result:

enter image description here

Upvotes: 1

Views: 283

Answers (1)

yrodiere
yrodiere

Reputation: 9977

Updated index manually fullTextSession.index(obj). -> DON'T WORK

If that doesn't work, and you're passing the "address" object, it's not related to the @IndexedEmbedded/@ContainedIn.

Code to update entity and index:

This code doesn't update the places field in Address. Hibernate Search may be reindexing Address, but since the address currently in the Hibernate session has an empty places field, well...

Try adding the new places to Address.places in your code, it should work.

Upvotes: 2

Related Questions