Skip to content

Custom converter for multibuckets #1141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
moriyahazan opened this issue May 27, 2021 · 6 comments · Fixed by #1148
Closed

Custom converter for multibuckets #1141

moriyahazan opened this issue May 27, 2021 · 6 comments · Fixed by #1148
Labels
type: bug A general bug

Comments

@moriyahazan
Copy link

i implemented multibuckets according to explained in this issue but i can't add custom converters to all buckets, the converters added for default bucket only

please see my code:

Custome converter

 @Override
   public CouchbaseCustomConversions customConversions() {
       return new CouchbaseCustomConversions(Arrays.asList(
               ZonedDateTimeToEpochTimeConverter.INSTANCE,
               EpochTimeToZonedDateTimeConverter.INSTANCE));
   }


   @WritingConverter
   public enum ZonedDateTimeToEpochTimeConverter implements Converter<ZonedDateTime, CouchbaseDocument> {
       INSTANCE;

       @Override
       public CouchbaseDocument convert(ZonedDateTime zonedDateTime) {
           CouchbaseDocument cd = new CouchbaseDocument();
           cd.put(EPOC_MILLI, zonedDateTime.toInstant().toEpochMilli());
           cd.put(OFFSET_SECONDS, zonedDateTime.getOffset().getTotalSeconds());
           return cd;
       }
   }

   @ReadingConverter
   public enum EpochTimeToZonedDateTimeConverter implements Converter<CouchbaseDocument, ZonedDateTime> {
       INSTANCE;

       @Override
       public ZonedDateTime convert(CouchbaseDocument epochTime) {
           long timeMilli = Long.parseLong(epochTime.getContent().get(EPOC_MILLI).toString());
           int offsetSeconds = Integer.parseInt(epochTime.getContent().get(OFFSET_SECONDS).toString());

           ZoneOffset convertedOffset = ZoneOffset.ofTotalSeconds(offsetSeconds);
           return ZonedDateTime.ofInstant(Instant.ofEpochMilli(timeMilli), convertedOffset);
       }
   }

add converter to the costum mapping:


MappingCouchbaseConverter mappingCouchbaseConverter = new MappingCouchbaseConverter();
mappingCouchbaseConverter.setCustomConversions(customConversions());
baseMapping.mapEntity(Class.forName(beanDefinition.getBeanClassName()),myCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),mappingCouchbaseConverter ));

I am getting below exception when save the doc although there is convertor from zonedatetime to couchebasedocument


2021-05-27 17:29:27.178 ERROR 18916 --- [nio-9001-exec-1] g.k.e.error.DefaultGraphQLErrorHandler   : Error executing query Exception while fetching data (/createProduct) : No converter found capable of converting from type [java.time.ZonedDateTime] to type [org.springframework.data.couchbase.core.mapping.CouchbaseDocument]

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.time.ZonedDateTime] to type [org.springframework.data.couchbase.core.mapping.CouchbaseDocument]
   at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:322) ~[spring-core-5.3.5.jar:5.3.5]
   at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195) ~[spring-core-5.3.5.jar:5.3.5]
   at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:175) ~[spring-core-5.3.5.jar:5.3.5]
   at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.lambda$getPotentiallyConvertedSimpleWrite$2(MappingCouchbaseConverter.java:784) ~[spring-data-couchbase-4.1.6.jar:4.1.6]
   at java.base/java.util.Optional.map(Optional.java:265) ~[na:na]
   at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.getPotentiallyConvertedSimpleWrite(MappingCouchbaseConverter.java:784) ~[spring-data-couchbase-4.1.6.jar:4.1.6]
   at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.writeSimpleInternal(MappingCouchbaseConverter.java:774) ~[spring-data-couchbase-4.1.6.jar:4.1.6]
   at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.access$200(MappingCouchbaseConverter.java:82) ~[spring-data-couchbase-4.1.6.jar:4.1.6]

Can you please help?

@mikereiche
Copy link
Collaborator

Please provide a complete example. Or coordinates to the github project.

@mikereiche
Copy link
Collaborator

mikereiche commented Jun 3, 2021

From #878 "@mikereiche I tried to use special name for converter but issue still persist
please see code in https://github.com/moriyahazan/designer-service"

The problem is that the converters are not being registered with the conversion service. There is a workaround below.
When not using repository mapping, the template is created with the mappingCouchbaseConverter Bean, and the spring framework call its afterPropertiesSet() method, which in turn registers the converters with the conversion service.

@Override
public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
    MappingCouchbaseConverter mappingCouchbaseConverter = new MappingCouchbaseConverter();
    mappingCouchbaseConverter.setCustomConversions(customBucketsConversions());
    mappingCouchbaseConverter.afterPropertiesSet(); // <<<< necessary to register with conversionService
    CouchbaseTemplate projectTemplate = customCouchbaseTemplate(customCouchbaseClientFactory("project_manager"), mappingCouchbaseConverter);
    baseMapping.mapEntity(Project.class, projectTemplate); 
}

@moriyahazan
Copy link
Author

works, thank you!

@mikereiche mikereiche added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 3, 2021
@mikereiche
Copy link
Collaborator

It would probably be a good idea for all the templates to use the mappingCouchbaseConverter Bean (instead of creating their own).

@mikereiche
Copy link
Collaborator

setCustomConverters() should call afterPropertiesSet() to register the converters.

mikereiche added a commit that referenced this issue Jun 8, 2021
1) have AbstractCouchbaseConverter.setCustomConversions call
afterPropertiesSet() to register converters with conversionService

2) have MappingCouchbaseConverter constructor with mappingContext
set its applicationContext from the applicationContext from mappingContext

3) add getApplicationContext() to CouchbaseMappingContext to use
in setting applicationContext of MappingCouchbaseConverter

4) change examples in Config to show creating repository mappings using
existing MappingCouchbaseConverter (which have customConversions /
BeanNames.COUCHBASE_CUSTOM_CONVERSIONS and applicationContext)
instead of creating their own and then adding the customConversion and
applicationContext.

5) remove unused members fro StringBasedN1qlQueryParser

Closes #1141.
mikereiche added a commit that referenced this issue Jun 8, 2021
1) have AbstractCouchbaseConverter.setCustomConversions call
afterPropertiesSet() to register converters with conversionService

2) have MappingCouchbaseConverter constructor with mappingContext
set its applicationContext from the applicationContext from mappingContext

3) add getApplicationContext() to CouchbaseMappingContext to use
in setting applicationContext of MappingCouchbaseConverter

4) change examples in Config to show creating repository mappings using
existing MappingCouchbaseConverter (which have customConversions /
BeanNames.COUCHBASE_CUSTOM_CONVERSIONS and applicationContext)
instead of creating their own and then adding the customConversion and
applicationContext.

5) remove unused members fro StringBasedN1qlQueryParser

Closes #1141.
mikereiche added a commit that referenced this issue Jun 8, 2021
1) have AbstractCouchbaseConverter.setCustomConversions call
afterPropertiesSet() to register converters with conversionService

2) have MappingCouchbaseConverter constructor with mappingContext
set its applicationContext from the applicationContext from mappingContext

3) add getApplicationContext() to CouchbaseMappingContext to use
in setting applicationContext of MappingCouchbaseConverter

4) change examples in Config to show creating repository mappings using
existing MappingCouchbaseConverter (which have customConversions /
BeanNames.COUCHBASE_CUSTOM_CONVERSIONS and applicationContext)
instead of creating their own and then adding the customConversion and
applicationContext.

5) replace @Autowired couchbaseObjectMapper with couchbaseObjectMapper()
to avoid the following when using @componentscan :
Unsatisfied dependency expressed through field 'couchbaseObjectMapper';
nested exception is org.springframework.beans.factory.
BeanCurrentlyInCreationException: Error creating bean with name
'couchbaseObjectMapper': Requested bean is currently in creation: Is
there an unresolvable circular reference?

6) remove unused members fro StringBasedN1qlQueryParser

Closes #1141.
mikereiche added a commit that referenced this issue Jun 10, 2021
1) change examples in Config to show creating repository mappings using
existing MappingCouchbaseConverter (which have customConversions /
BeanNames.COUCHBASE_CUSTOM_CONVERSIONS and applicationContext)
instead of creating their own and then adding the customConversion and
applicationContext.

2) replace @Autowired couchbaseObjectMapper with couchbaseObjectMapper()
to avoid the following when using @componentscan :
Unsatisfied dependency expressed through field 'couchbaseObjectMapper';
nested exception is org.springframework.beans.factory.
BeanCurrentlyInCreationException: Error creating bean with name
'couchbaseObjectMapper': Requested bean is currently in creation: Is
there an unresolvable circular reference?

3) remove unused members fro StringBasedN1qlQueryParser

Closes #1141.
mikereiche added a commit that referenced this issue Jun 10, 2021
1) change examples in Config to show creating repository mappings using
existing MappingCouchbaseConverter (which have customConversions /
BeanNames.COUCHBASE_CUSTOM_CONVERSIONS and applicationContext)
instead of creating their own and then adding the customConversion and
applicationContext.

2) replace @Autowired couchbaseObjectMapper with couchbaseObjectMapper()
to avoid the following when using @componentscan :
Unsatisfied dependency expressed through field 'couchbaseObjectMapper';
nested exception is org.springframework.beans.factory.
BeanCurrentlyInCreationException: Error creating bean with name
'couchbaseObjectMapper': Requested bean is currently in creation: Is
there an unresolvable circular reference?

3) remove unused members fro StringBasedN1qlQueryParser

Closes #1141.

Co-authored-by: mikereiche <[email protected]>
mikereiche added a commit that referenced this issue Aug 9, 2021
1) change examples in Config to show creating repository mappings using
existing MappingCouchbaseConverter (which have customConversions /
BeanNames.COUCHBASE_CUSTOM_CONVERSIONS and applicationContext)
instead of creating their own and then adding the customConversion and
applicationContext.

2) replace @Autowired couchbaseObjectMapper with couchbaseObjectMapper()
to avoid the following when using @componentscan :
Unsatisfied dependency expressed through field 'couchbaseObjectMapper';
nested exception is org.springframework.beans.factory.
BeanCurrentlyInCreationException: Error creating bean with name
'couchbaseObjectMapper': Requested bean is currently in creation: Is
there an unresolvable circular reference?

3) remove unused members fro StringBasedN1qlQueryParser

Closes #1141.

Co-authored-by: mikereiche <[email protected]>
@JesusTheHun
Copy link

@mikereiche I'm unsure of the state of this, has your improvement been merged ? if so in what version and what is the current best solution for this ?
Also the multi bucket doc is still missing afaik. Who do we need to pressure to allocate resource to this ? :D jk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants