javaravendbravendb4

Is there a way to specify collection name for Java bean being stored into RavenDB?


I'm using RavenDB with JAVA ravendb-jvm-client for my app, where existing collection names does'n reflect 'User.java' -> 'Users' convention. Is there a way how to specifiy correct collection name for java beans so java client use this instead of automatic convention? Something similar to @Table annotation for JPA.

I know I can specify collection name for example in queries e.g. session.query(User.class, Query.collection("custUsers")); ... but its very verbose to specify it repeatedly.


Solution

  • You need to use FindCollectionName convention of the DocumentStore.

    In Java, you can use setFindCollectionName of the conventions.

    From https://ravendb.net/docs/article-page/4.1/java/client-api/session/configuration/how-to-customize-collection-assignment-for-entities :

    By default a collection name is pluralized form of a name of an entity type. For example objects of type Category will belong to Categories collection. However if your intention is to classify them as ProductGroups use the following code:

    store.getConventions().setFindCollectionName(clazz -> {
        if (Category.class.isAssignableFrom(clazz)) {
            return "ProductGroups";
        }
    
        return DocumentConventions.defaultGetCollectionName(clazz);
    });
    

    Documentation on conventions in general: https://ravendb.net/docs/article-page/4.1/java/client-api/configuration/identifier-generation/global