feedback icon indicating copy to clipboard operation
feedback copied to clipboard

[cloud-spring-datastore]: ID property not found for Hashset while using Example.of query

Open rajeshvaghela opened this issue 2 years ago • 1 comments

  1. I have following entity @Entity @Data public class Book { @Id Long id; String title; @Reference Set<Chapter> chapters = new HashSet<>(); }

  2. if I search books with title using Example.of query (not setting any value for chapters), I am getting below error

com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException: An ID property was required but does not exist for the type: class java.util.HashSet at com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntityImpl.getIdPropertyOrFail(DatastorePersistentEntityImpl.java:135) at com.example.demo.keys.SaccObjectToKeyFactory.getKeyFromObject(SaccObjectToKeyFactory.java:64) at com.google.cloud.spring.data.datastore.core.DatastoreTemplate.lambda$exampleToQuery$22(DatastoreTemplate.java:923) at org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:317) at com.google.cloud.spring.data.datastore.core.DatastoreTemplate.exampleToQuery(DatastoreTemplate.java:912) at com.google.cloud.spring.data.datastore.core.DatastoreTemplate.queryByExample(DatastoreTemplate.java:390) at com.google.cloud.spring.data.datastore.core.DatastoreTemplate.queryByExample(DatastoreTemplate.java:100) at com.google.cloud.spring.data.datastore.repository.support.SimpleDatastoreRepository.findAll(SimpleDatastoreRepository.java:189) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  1. I believe root cause is at below location com.google.cloud.spring.data.datastore.core.DatastoreTemplate.exampleToQuery(Example<T>, DatastoreQueryOptions, boolean)

persistentEntity.doWithAssociations( (AssociationHandler<DatastorePersistentProperty>) association -> { PersistentPropertyAccessor> accessor = persistentEntity.getPropertyAccessor(example.getProbe()); DatastorePersistentProperty property = association.getInverse(); Object value = accessor.getProperty(property); Value> key = value == null ? NullValue.of() : KeyValue.of( objectToKeyFactory.getKeyFromObject( value, getPersistentEntity(value.getClass()))); addFilter(nullHandler, filters, property.getFieldName(), key); });

function check for null but do not check for empty set.

rajeshvaghela avatar May 21 '23 02:05 rajeshvaghela

jar version .m2\repository\com\google\cloud\spring-cloud-gcp-data-datastore\4.3.0\spring-cloud-gcp-data-datastore-4.3.0.jar

rajeshvaghela avatar May 21 '23 02:05 rajeshvaghela