3

I changed the way that Spring Data Couchbase writes its _class attribute following this answer. Now, if I save an object of class com.package.entity.User, my document looks like:

{
    ...
    "_type": "user"
}

My point is, when I use query methods like public Long countByAdminIsTrue(), the request generated by Spring is the following:

SELECT COUNT(*) FROM `myBucket` WHERE (`admin` = TRUE) AND `_type` = "com.package.entity.User"

Instead of the result I expect:

SELECT COUNT(*) FROM `myBucket` WHERE (`admin` = TRUE) AND `_type` = "user"

Here is my CouchbaseTypeMapper doint this work:

public class CustomCouchbaseTypeMapper extends DefaultTypeMapper<CouchbaseDocument> implements CouchbaseTypeMapper {
    public CustomCouchbaseTypeMapper() {
        super(new CustomCouchbaseDocumentTypeAliasAccessor());
    }

    @Override
    public String getTypeKey() {
        return "_type";
    }

    public static final class CustomCouchbaseDocumentTypeAliasAccessor implements TypeAliasAccessor<CouchbaseDocument> {
        @Override
        public Object readAliasFrom(CouchbaseDocument source) {
            return source.get("_type");
        }

        @Override
        public void writeTypeTo(CouchbaseDocument sink, Object alias) {
            String typeName = StringUtils.typeNameFromFullClassName((String) alias);
            sink.put("_type", typeName);
        }
    }
}

I already tried to define a ConfigurableTypeInformationMapper in the constructor with my entities, it doesn't help. How can I make Spring Data to use my custom type name and value when it generates a query based on a query method?

Community
  • 1
  • 1
Happy
  • 1,815
  • 2
  • 18
  • 33
  • Ensure you it's not a typo but `formattedTypeName` is not defined – Troopers May 10 '17 at 14:10
  • Why are you avoiding using a fully qualified classname? – prettyvoid Aug 21 '17 at 09:17
  • 2
    Because this bucket is shared with others apps which are not using Spring Data / are not Java apps / does not have their entities in the same package / etc. – Happy Aug 22 '17 at 08:08
  • Note after about 1 year as a developer on this application : do not share buckets with other apps. Use web services instead. – Happy Sep 20 '18 at 08:45

0 Answers0