Problem with massindexer and @IndexedEmbedded

Hello,
i have 2 classes ObjectA and ObjectB.

@Entity
@Indexed(index = "idx_objectA")
@Table(name = "objectA")
public class ObjectA {
    .
    .
    .	
	@OneToOne(cascade = CascadeType.ALL)
	@JoinColumn(name = "objectBid", referencedColumnName = "id")
	private ObjectB objectB;
}

@Entity
@Indexed(index = "idx_objectB")
@Table(name = "objectB")
public class ObjectB {
	.
    .
    .	
	@OneToOne(mappedBy = "objectB")
	private ObjectA objectA;
}

Both of them i dont use @IndexedEmbedded

For mass indexer i use the below

final MassIndexer indexer = searchSession.massIndexer(ObjectA.class/*or ObjectB.class*/)
				.purgeAllOnStart(true)
				.mergeSegmentsAfterPurge(true)
				.batchSizeToLoadObjects(100)
				.threadsToLoadObjects(12)
				.typesToIndexInParallel(1)
				.idFetchSize(250);
indexer.startAndWait();

i have almost 2M records on my database both ObjectA and ObjectB.

In the debug mode during the massindexer i get the below sql too many times but I can’t understand why, since I don’t use the @IndexedEmbedded. Without @IndexedEmbedded, expecting it to be skipped.

DEBUG- 2023-07-24 15:01:03.228 28780-[ity loading - 7] o.h.SQL  select objectA.id from objectA  where objectA.objectB.id = ?
DEBUG- 2023-07-24 15:01:03.231 28780-[ity loading - 9] o.h.SQL  select objectB.id from objectB  where objectB.id = ?

Hey Tony,

Thanks for reaching out. I can’t say for sure based on the info you’ve provided, but it may be since @OneToOne is fetch==EAGER by default or because your debugger is trying to render your object and makes that additional call to fetch the other entity … Could you try building a small reproducer using this template https://github.com/hibernate/hibernate-test-case-templates/tree/main/search/hibernate-search-6/orm-lucene

1 Like

Removing @OneToOne from ObjectB, my problem is resolved. But I can’t understand why it creates select sql over a property that I don’t want to keep index and i don’t use @IndexedEmbedded :confused:

Hey Tony,

This is not necessarily coming from what Search is doing but most likely from how Hibernate ORM loads the entity. I’ve tried running a simple example based on the entities you’ve provided, and I do not get an additional SELECT. Instead, I have a select with a join to the other entity when it is loaded (which is expected since those one-to-one associations are eager by default). Instead of removing the OneToOne you could try setting the fetch to LAZY if you have bytecode enhancement enabled. But since I don’t observe the same behaviour you describe in the example I’ve been testing against, I can’t say much if I don’t have a reproducer.

1 Like