# Key-value Template

The key value is a bridge between the entity and the key-value database.

## `KeyValueTemplate`

The `KeyValueTemplate` is the column template to synchronous tasks. It has three components:

The KeyValueTemplate is responsible for persistency of an entity in a key-value database. It is composed basically for three components.

* **KeyValueEntityConverter**: That converts an entity to communication API, e.g., The Person to KeyValueEntity.
* **BucketManager**: The Diana column key-value entity manager.
* **KeyValueWorkflow**: The workflow to update and save methods.

```java
KeyValueTemplate template = null;
User user = new User();
user.setNickname("ada");
user.setAge(10);
user.setName("Ada Lovelace");
List<User> users = Collections.singletonList(user);

template.put(user);
template.put(users);

Optional<Person> ada = template.get("ada", Person.class);
Iterable<Person> usersFound = template.get(Collections.singletonList("ada"), Person.class);
```

To use a key-value template just follows the CDI style and put an `@Inject` on the field.

```java
@Inject
private KeyValueTemplate template;
```

The next step is to produce a **BucketManager**:

```java
@Produces
public BucketManager getManager() {
    BucketManager manager = //instance
    return manager;
}
```

To work with more than one key-value Template, there are two approaches:

1\) Using qualifieres:

```java
    @Inject
    @Database(value = DatabaseType.KEY_VALUE, provider = "databaseA")
    private KeyValueTemplate templateA;

    @Inject
    @Database(value = DatabaseType.KEY_VALUE, provider = "databaseB")
    private KeyValueTemplate templateB;


    //producers methods
    @Produces
    @Database(value = DatabaseType.KEY_VALUE, provider = "databaseA")
    public BucketManager getManagerA() {
        DocumentCollectionManager manager =//instance
        return manager;
    }

    @Produces
    @Database(value = DatabaseType.KEY_VALUE, provider = "databaseB")
    public DocumentCollectionManager getManagerB() {
        BucketManager manager = //instance
        return manager;
    }
```

2\) Using the **KeyValueTemplateProducer** class

```java
@Inject
private KeyValueTemplateProducer producer;

public void sample() {
   BucketManager managerA = //instance;
   BucketManager managerB = //instance
   KeyValueTemplate templateA = producer.get(managerA);
   KeyValueTemplate templateB = producer.get(managerB);
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jnosql.gitbook.io/jnosql-book/en/part4/chapter4_2_3.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
