jlb jlb - 4 months ago 31x
Java Question

SolrCrudRepository : use inheritance

using java8, spring-boot 1.3.5, SOLR 5.5.1

I'm trying to save into the same schema several distinct objects, using SolrCrudRepository .
At first, I only had object A, so I used

public interface SolrRepository extends SolrCrudRepository<A , String>
..... //in the service class :
private SolrRepository solrRepository;

And that was it. Advancing on the project, I was asked to index another object B (related to A from a business pov, but only that) . So, as I did not want to create anther solr repo, I made A and B extends another abstract class AbstractSolrPOJO, and use instead :

public interface SolrRepository extends SolrCrudRepository<AbstractSolrPojo , String>

And that does not work. The autowired of the solr repo fails :

Caused by: java.lang.NullPointerException: null
at org.springframework.data.solr.repository.support.MappingSolrEntityInformation.getIdAttribute(MappingSolrEntityInformation.java:51)
at org.springframework.data.solr.repository.support.SimpleSolrRepository.<init>(SimpleSolrRepository.java:81)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)

It seems that spring is looking for the id property, that is defined in both classes using @Field("id") annotation.

Is moving that id to the superclass did not work, I tried with an interface, providing a method to override

String getIndexId();

but that fails too, but with another error :

Caused by: org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Document is missing mandatory uniqueKey field

So, do I really have to create one repository per object, or is there a "trick" here ?

Thank you

jlb jlb

Ok so, I'm an idiot ! my SOLR schema was wrong (the unique key was not properly defined ). The superclass way works

So, for those interested : - define in the schema a unique key :

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
  • put the key definition in the superclass (the @Field("id") annotation should be there )
  • be carefull to not have an attribute named "id" in the classes extending your superclass. If you have to add a 'functionnal' id to your class (the id of the object, different from the solr id) then don't forget to annotate it with

    @Indexed(readonly = true, stored = false, searchable = false)