org.hibernate.HibernateException: OGM000080: Could not load an entity batch

I have this class structure:

@Entity
@Table(name="BigEvent")
@DiscriminatorColumn(name = "type")
public class GridListEvent extends BigEvent implements Serializable{

	@OneToOne(cascade=CascadeType.ALL)
	@EventBody
	private GridListEventBody ogmEventBody;
	...
}
@Entity
@Table(name="GridListEventBody")
public class GridListEventBody implements Serializable{

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	@GridFSBucket("gridlist")
    GridFS gridlist;	
	...
}

A jpql query like this works
Query query = mdm.getEntityManager().createQuery("FROM GridListEvent e WHERE e.id=:id").query.setParameter("id",1L);

If I want to select all the entities it doesn’t with the error in title
Query query = mdm.getEntityManager().createQuery("SELECT e FROM GridListEvent e");

As you can see the GridFS field is in Body entity, I query for the main entity GridListEvent.
I noticed it works if I move the GridFS field in GridListEvent but I dont’ need that.

This is the stacktrace

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.HibernateException: OGM000080: Could not load an entity batch: [com.actor.jpa.GridListEventBody#<2, 4>]
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1514)
	at org.hibernate.query.Query.getResultList(Query.java:135)
	at com.actor.benchmark.gridfs.Select.main(Select.java:39)
Caused by: org.hibernate.HibernateException: OGM000080: Could not load an entity batch: [com.actor.jpa.GridListEventBody#<2, 4>]
	at org.hibernate.ogm.loader.impl.OgmLoader.loadEntityBatch(OgmLoader.java:1324)
	at org.hibernate.ogm.loader.entity.impl.BatchingEntityLoader.doBatchLoad(BatchingEntityLoader.java:99)
	at org.hibernate.ogm.loader.entity.impl.DynamicBatchingEntityLoaderBuilder$DynamicBatchingEntityLoader.load(DynamicBatchingEntityLoaderBuilder.java:124)
	at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4269)
	at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:511)
	at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:481)
	at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:222)
	at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:281)
	at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:124)
	at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:92)
	at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1257)
	at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1140)
	at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:682)
	at org.hibernate.type.EntityType.resolve(EntityType.java:464)
	at org.hibernate.type.ManyToOneType.resolve(ManyToOneType.java:239)
	at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:172)
	at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
	at org.hibernate.ogm.loader.impl.OgmLoader.initializeEntitiesAndCollections(OgmLoader.java:810)
	at org.hibernate.ogm.loader.impl.OgmLoader.doQuery(OgmLoader.java:394)
	at org.hibernate.ogm.loader.impl.OgmLoader.doQueryAndInitializeNonLazyCollections(OgmLoader.java:284)
	at org.hibernate.ogm.loader.impl.OgmLoader.loadEntity(OgmLoader.java:202)
	at org.hibernate.ogm.loader.impl.OgmLoader.loadEntitiesFromTuples(OgmLoader.java:221)
	at org.hibernate.ogm.loader.entity.impl.DynamicBatchingEntityLoaderBuilder$DynamicBatchingEntityLoader.loadEntitiesFromTuples(DynamicBatchingEntityLoaderBuilder.java:129)
	at org.hibernate.ogm.query.impl.OgmQueryLoader.listOfEntities(OgmQueryLoader.java:92)
	at org.hibernate.ogm.query.impl.OgmQueryLoader.list(OgmQueryLoader.java:78)
	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:504)
	at org.hibernate.ogm.query.impl.OgmQueryTranslator.list(OgmQueryTranslator.java:130)
	at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:220)
	at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1508)
	at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1537)
	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1505)
	... 2 more
Caused by: org.hibernate.PropertyAccessException: Could not set field value [5c4dd4f3c0f6832d6caba88f] value by reflection : [class com.actor.jpa.GridListEventBody.gridlist] setter of com.actor.jpa.GridListEventBody.gridlist
	at org.hibernate.property.access.spi.SetterFieldImpl.set(SetterFieldImpl.java:61)
	at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:649)
	at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:205)
	at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:4903)
	at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:190)
	at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
	at org.hibernate.ogm.loader.impl.OgmLoader.initializeEntitiesAndCollections(OgmLoader.java:810)
	at org.hibernate.ogm.loader.impl.OgmLoader.doQuery(OgmLoader.java:394)
	at org.hibernate.ogm.loader.impl.OgmLoader.doQueryAndInitializeNonLazyCollections(OgmLoader.java:284)
	at org.hibernate.ogm.loader.impl.OgmLoader.loadEntityBatch(OgmLoader.java:1321)
	... 32 more
Caused by: java.lang.IllegalArgumentException: Can not set org.hibernate.ogm.datastore.mongodb.type.GridFS field com.actor.jpa.GridListEventBody.gridlist to org.bson.types.ObjectId
	at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
	at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
	at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
	at java.base/java.lang.reflect.Field.set(Field.java:780)
	at org.hibernate.property.access.spi.SetterFieldImpl.set(SetterFieldImpl.java:41)
	... 41 more

I tried to select just 2 entities

Query query = mdm.getEntityManager().createQuery("SELECT e FROM GridListEvent e WHERE e.id IN :ids");
List<Long> ids = new LinkedList<Long>();
ids.add(1L);
ids.add(2L);
query.setParameter("ids",ids);
List results = query.getResultList();

I get the same error.

I’m checking, no idea at the moment what can be the problem.

I noticed if I completely remove the entity GridListEventBody and move the GridFS field in GridListEvent those query works and the GridFS data are loaded correctly…but this would change the data model, I would avoid that.

Yes, I think it’s a bug in OGM. I’m checking for a workaround.

I can confirm that this is a bug in OGM. When the attribute is recovered from an association is not converted correctly. The error is caused because the value is still an ObjectId when OGM tries to assign it to the field.

The problem is that right now I don’t have the time to fix it. I’ve created this issue though: https://hibernate.atlassian.net/browse/OGM-1553

If you want to try to fix it, let me know.