Spring Data Neo4j - Indexing and Inheritance

浪子不回头ぞ 提交于 2019-12-11 07:23:45

问题


Lets say i have the following data model:

public class A {

    @Indexed(indexType = IndexType.FULLTEXT, indexName = "property1")
    String property1;
}

public class B extends A {

    @Indexed(indexType = IndexType.FULLTEXT, indexName = "property2")
    String property2;
}

Can i tell the Spring framework to index property1 of class B under a different index name? If not, what would you do in such case? I mean, what would you do if you have few classes that all extends the same base class, but in the same time, all the properties that those classes inherit form the base class should be indexed. I can annotate those properties for indexing only in the base class, and it is very limiting. What can i do?

Thanks.


回答1:


I don't think that's possible. Your property1 will always be indexed in index property1. Being able to specify multiple indexes on a single field would probably fix your issue, but it's currently not possible. A while ago, I've raised an issue for this, but it's not yet implemented.

If you really want a domain (entity) object approach, you could also opt for the domain entity approach. It's not related to Spring or Spring Data Neo4j, but it also does the trick. By manually handling your entities this way, you could also manage the indexes yourself, thus gaining all the flexibility you want.

Just a question, why would you want to specify a different index per subclass?




回答2:


The level attribute in the index definition annotation can be set to Level.INSTANCE. For more help please refer spring-data-neo4j documentation here

Here is an excerpt from the doc :

If a field is declared in a superclass but different indexes for subclasses are needed, the level attribute declares what will be used as index. Level.CLASS uses the class where the field was declared and Level.INSTANCE uses the class that is provided or of the actual entity instance.



来源:https://stackoverflow.com/questions/16300276/spring-data-neo4j-indexing-and-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!