How to use Spring data with couchbase without the _class property

Is there an easy way to use spring data couchbase with documents without the _class attribute?
In the sofa base, there is something like this in my sampling database:

{
"username": "alice",
"created": 1473292800000,
"data": {"a": 1, "b": "2"},
"type": "mydata"
}

Now, is there a way to define the mapping from this document structure to Java objects (note that the _class attribute is missing and cannot be added), and vice versa, so that I get all (or most) automation from the spring couchbase data Function?

It’s like:
If the value of the type field is "mydata", the class MyData.java is used.
So, when find is executed instead of automatically adding AND _class = "mydata" Add AND type = "mydata" to the generated query.

Spring Data usually requires the _class field To know the content to be instantiated during deserialization.

By overriding the typeKey() method in AbsctractCouchbaseDataConfiguration, it is quite easy to use a field name different from _class in Spring Data Couchbase.

But it still expects the fully qualified class name to be there by default

Bypassing this will require more work:

>You need to follow the model of DefaultCouchbaseTypeMapper Implement your own CouchbaseTypeMapper. In the super(...) constructor, you need to provide an additional parameter: TypeInformationMapper list. The default implementation does not provide one explicitly, so use SimpleTypeInformationMapper, which is the one where FQN is placed.> There is also a configurable Alternative implementation, so you can use Map: ConfigurableTypeInformationMapper to name a specific category with a shorter name... 3. Therefore, by placing the ConfigurableTypeInformationMapper with the alias required for the specific class after the SimpleTypeInformationMapper in the list (the alias is not provided for your serialization In the case of the class), you can achieve your goal.> TypeMapper is used in MapCobaseConverter, unfortunately you also need to extend it (just to instantiate your typeMapper instead of the default value).> When you are done, overwrite the configuration again to return An instance of a custom MappingCouchbaseConverter using a custom CouchbaseTypeMapper (mappingCouchbaseConverter() method).

Is there a simple way to combine spring data couchbase with a document without _class attribute use together?
In the sofa base, there is something like this in my sampling database:

{
"username": "alice",
"created": 1473292800000,
"data": {"a": 1, "b": "2"},
"type": "mydata"
}

Now, is there a way to define the mapping from this document structure to Java objects (note that the _class attribute is missing and cannot be added), and vice versa, so that I get all (or most) automation from the spring couchbase data Function?

It’s like:
If the value of the type field is "mydata", the class MyData.java is used.
So, when find is executed instead of automatically adding AND _class = "mydata" Add AND type = "mydata" to the generated query.

Spring Data usually needs the _class field to know what to instantiate during deserialization.

By overriding the typeKey() method in AbsctractCouchbaseDataConfiguration, it is quite easy to use a field name different from _class in Spring Data Couchbase.

But it still expects to be in by default There are fully qualified class names.

It will take more work to bypass this:

>You need to implement your own CouchbaseTypeMapper according to the model of DefaultCouchbaseTypeMapper. In the super(...) constructor You need to provide an additional parameter: a list of TypeInformationMapper. The default implementation does not provide one explicitly, so use SimpleTypeInformationMapper, which is the one that puts the FQN.> There is also a configurable alternative implementation, so you can pass Map: ConfigurableTypeInformationMapper to The specific category name is a shorter name... 3. Therefore, you can achieve your goal by placing the ConfigurableTypeInformationMapper with the alias required by the specific class after the SimpleTypeInformationMapper in the list (for the case where you serialize the class for which no alias is provided).> TypeMapper is used in MapCobaseConverter, unfortunately you also need to extend it (just to instantiate your typeMapper instead of the default value).> When you are done, overwrite the configuration again to return to the custom CouchbaseTypeMapper (mappingCouchbaseConverter() method) Define an instance of MappingCouchbaseConverter.

Leave a Comment

Your email address will not be published.