|
2 | 2 | = Template & direct operations
|
3 | 3 |
|
4 | 4 | The template provides lower level access to the underlying database and also serves as the foundation for repositories.
|
5 |
| -Any time a repository is too high-level for you needs chances are good that the templates will serve you well. |
| 5 | +Any time a repository is too high-level for you needs chances are good that the templates will serve you well. Note that |
| 6 | +you can always drop into the SDK directly through the beans exposed on the `AbstractCouchbaseConfiguration`. |
6 | 7 |
|
7 | 8 | [[template.ops]]
|
8 | 9 | == Supported operations
|
9 | 10 |
|
10 |
| -The template can be accessed through the `couchbaseTemplate` bean out of your context. |
| 11 | +The template can be accessed through the `couchbaseTemplate` and `reactiveCouchbaseTemplate` beans out of your context. |
11 | 12 | Once you've got a reference to it, you can run all kinds of operations against it.
|
12 | 13 | Other than through a repository, in a template you need to always specify the target entity type which you want to get converted.
|
13 | 14 |
|
14 |
| -To mutate documents, you'll find `save`, `insert` and `update` methods exposed. |
15 |
| -Saving will insert or update the document, insert will fail if it has been created already and update only works against documents that have already been created. |
| 15 | +The templates use a fluent-style API which allows you to chain in optional operators as needed. As an example, here is |
| 16 | +how you store a user and then find it again by its ID: |
16 | 17 |
|
17 |
| -Since Couchbase Server has different levels of persistence (by default you'll get a positive response if it has been acknowledged in the managed cache), you can provide higher durability options through the overloaded `PersistTo` and/or `ReplicateTo` options. |
18 |
| -The behaviour is part of the Couchbase Java SDK, please refer to the official documentation for more details. |
19 |
| - |
20 |
| -Removing documents through the `remove` methods works exactly the same. |
21 |
| - |
22 |
| -If you want to load documents, you can do that through the `findById` method, which is the fastest and if possible your tool of choice. |
23 |
| -The find methods for views are `findByView` which converts it into the target entity, but also `queryView` which exposes lower level semantics. |
24 |
| -Similarly, find methods using N1QL are provided in `findByN1QL` and `queryN1QL`. |
25 |
| -Additionally, since N1QL allows you to select specific fields in documents (or even across documents using joins), `findByN1QLProjection` will allow you to skip full `Document` conversion and map these fields to an ad-hoc class. |
26 |
| - |
27 |
| -WARNING: If it is detected at runtime that the cluster doesn't support N1QL, these methods will throw a `UnsupportedCouchbaseFeatureException`. |
28 |
| - |
29 |
| -If you really need low-level semantics, the `couchbaseBucket` is also always in scope through `getCouchbaseBucket()`. |
30 |
| - |
31 |
| -[[couchbase.template.xml]] |
32 |
| -== Xml Configuration |
33 |
| - |
34 |
| -The template can be configured via xml, including setting a custom `TranslationService`. |
35 |
| - |
36 |
| -.XML Based Template Declaration |
| 18 | +.Fluent template access |
37 | 19 | ====
|
38 |
| -[source,xml] |
| 20 | +[source,java] |
39 | 21 | ----
|
40 |
| -<?xml version="1.0" encoding="UTF-8"?> |
41 |
| -<beans xmlns="http://www.springframework.org/schema/beans" |
42 |
| - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
43 |
| - xmlns:couchbase="http://www.springframework.org/schema/data/couchbase" |
44 |
| - xsi:schemaLocation="http://www.springframework.org/schema/data/couchbase https://www.springframework.org/schema/data/couchbase/spring-couchbase.xsd |
45 |
| - http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> |
| 22 | +// Create an Entity |
| 23 | +User user = new User(UUID.randomUUID().toString(), "firstname", "lastname"); |
46 | 24 |
|
47 |
| - <couchbase:env/> |
48 |
| - <couchbase:cluster/> |
49 |
| - <couchbase:clusterInfo/> |
50 |
| - <couchbase:bucket/> |
| 25 | +// Upsert it |
| 26 | +couchbaseTemplate.upsertById(User.class).one(user); |
51 | 27 |
|
52 |
| - <couchbase:template translation-service-ref="myCustomTranslationService"/> |
| 28 | +// Retrieve it again |
| 29 | +User found = couchbaseTemplate.findById(User.class).one(user.getId()); |
| 30 | +---- |
| 31 | +==== |
53 | 32 |
|
54 |
| - <bean id="myCustomTranslationService" class="org.springframework.data.couchbase.core.convert.translation.JacksonTranslationService"/> |
| 33 | +If you wanted to use a custom durability requirement for the `upsert` operation you can chain it in: |
55 | 34 |
|
56 |
| -</beans> |
| 35 | +.Upsert with durability |
| 36 | +==== |
| 37 | +[source,java] |
| 38 | +---- |
| 39 | +User modified = couchbaseTemplate |
| 40 | + .upsertById(User.class) |
| 41 | + .withDurability(DurabilityLevel.MAJORITY) |
| 42 | + .one(user); |
57 | 43 | ----
|
58 | 44 | ====
|
59 | 45 |
|
60 |
| -NOTE: In the example above most tags assume their default values, that is a localhost cluster and bucket "default". |
61 |
| -In production you would have to also provide specifics to these tags. |
| 46 | +In a similar fashion, you can perform a N1QL operation: |
62 | 47 |
|
| 48 | +.N1QL query on the template |
| 49 | +==== |
| 50 | +[source,java] |
| 51 | +---- |
| 52 | +final List<User> foundUsers = couchbaseTemplate |
| 53 | + .findByQuery(User.class) |
| 54 | + .consistentWith(QueryScanConsistency.REQUEST_PLUS) |
| 55 | + .all(); |
| 56 | +---- |
| 57 | +==== |
0 commit comments