Jeff S
Jeff S

Reputation: 11

key prefixes with documents in spring-data-couchbase repositories

I commonly use a prefix pattern when storing documents in couchbase. For example a user document might have a key like "user::myusername" and an order document might have a key of "order::1".

I'm new to spring-data and don't see a way to easily make it work with these prefixes. I can specify a field in my object like:

public class UserLogin {
    public static final String dbPrefix = "user_login::";
    @Id
    private String id;
    private String username;


.
.
.
    public void setUsername(String username) {
        this.username = username;
        this.id = dbPrefix + this.username;
    }
}

and have a Crud repository

public interface UserLoginRepo extends CrudRepository<UserLogin, String> {

}

This is an ok work around because I can:

...

userLoginRepo.save(login)

UserLogin login = userLoginRepo.findOne(UserLogin.dbPrefix + "someuser");

...

It would be really nice if there were some way to have the repository automatically use the prefix behind the scenes.

Upvotes: 1

Views: 511

Answers (1)

tacree
tacree

Reputation: 68

not sure if you are asking a question here. Spring-data doesn't have any built-in mechanism to do what you want. If you are using Spring-cache with couchbase this is possible since you can annotate your cached methods with naming patterns like this, for example, in spring cache you can do something like this on the persistence method:

@Cacheable(value = "myCache", key = "'user_login::'+#id")
public UserLogin getUserLogin()

Obviously though, this is Spring cache and not spring data, however both are supported in the same library.

Upvotes: 0

Related Questions