jsonschema2pojo-spring-data-couchbase
This project is a jsonschema2pojo extension dedicated to Spring Data Couchbase entities generation.
Features
Couchbase document
At the schema of an object level, it is possible to define a POJO as being a Couchbase document using the custom JSON property x-cb-document
.
- If missing, the value of this property is
true
if the schema is at root or if its parent is also a Couchbase document. - The
true
value is equivalent to{}
. - The schema of the content of this custom property is available here.
This property is responsible for generating the Document
annotation.
E.g., this schema:
{
"title": "Sample entity",
"type": "object",
"properties": {
"..."
}
}
Will produce:
@Document
public class Entity {
...
}
Some sub-properties are available to manage the annotation parameters (detailed in the annotation's documentation).
E.g., this schema:
{
"title": "Sample entity",
"type": "object",
"x-cb-document": {
"expiry": 2,
"expiryUnit": "MINUTES",
"touchOnRead": true
},
"properties": {
"..."
}
}
Will produce:
@Document(expiry = 2, expiryUnit = TimeUnit.MINUTES, touchOnRead = true)
public class Entity {
...
}
Composite indexes
A sub-property compositeIndexes
is available to declare indexes that will can be auto-generated by Spring Data Couchbase via the CompositeQueryIndex
annotation and its parameters (detailed in the annotation's documentation).
E.g., this schema:
{
"title": "Sample entity",
"type": "object",
"x-cb-document": {
"compositeIndexes": [
{
"fields": [
"field1",
"field12"
]
},
{
"fields": [
"field2",
"field3",
"field4"
],
"name": "idx_fields"
}
]
},
"properties": {
"..."
}
}
Will produce:
@Document(expiry = 2, expiryUnit = TimeUnit.MINUTES, touchOnRead = true)
@CompositeQueryIndex(fields = {
"field1",
"field2"
})
@CompositeQueryIndex(fields = {
"field3",
"field4",
"field5"
}, name = "idx_fields")
public class Entity {
...
}
You can find more information on Spring Data Couchbase index creation and how to activate it here here.
Exclude a POJO from Couchbase related elements
If x-cb-document
is false
, Couchbase related elements will be skipped for the generated class, also for its fields and its sub-objects.
Document id
At the property of an object level, it is possible to define a field as being a document id using the custom JSON property x-cb-id
.
- If missing, the value of this property is
false
. - The
true
value is equivalent to{}
. - The schema of the content of this custom property is available here.
This property is responsible for generating the Id
annotation.
E.g., this schema:
{
"..."
"properties": {
"..."
"id": {
"title": "Entity id",
"type": "string",
"x-cb-id": true
},
"..."
}
}
Will produce:
@Id
private String id;
Generated id
A sub-property generated
is available to manage the generating of the GeneratedValue
annotation and its parameters (detailed in the annotation's documentation).
E.g., this schema:
{
"..."
"properties": {
"..."
"id": {
"title": "Entity id",
"type": "string",
"format": "uuid",
"x-cb-id": {
"generated": true
}
},
"..."
}
}
Will produce:
@Id
@GeneratedValue
private UUID id;
And this schema:
{
"..."
"properties": {
"..."
"id": {
"title": "Entity id",
"type": "string",
"x-cb-id": {
"generated": {
"delimiter": "::",
"strategy": "USE_ATTRIBUTES"
}
}
},
"..."
}
}
Will produce:
@Id
@GeneratedValue(delimiter = "::", strategy = GenerationStrategy.USE_ATTRIBUTES)
private String id;
You can find more information on Spring Data Couchbase key generation here.
Document CAS
At the property of an object level, it is possible to define a field as being a document CAS (Compare And Swap) using the custom JSON property x-cb-cas
.
If missing, the value of this property is false
.
This property is responsible for generating the Version
annotation.
Note that the type of a CAS field must be Long
or long
. This can be achieved through a formatTypeMapping
or the useLongIntegers
option.
E.g., this schema:
{
"..."
"properties": {
"..."
"cas": {
"title": "Couchbase CAS",
"type": "integer",
"format": "int64",
"x-cb-cas": true
},
"..."
}
}
Will produce:
@Version
private Long cas;
Document field
At the property of an object level, it is possible to define a field as being a document id using the custom JSON property x-cb-field
.
- If missing and if the field is not already marked as being an id, a cas or a join, the value of this property is
true
. - If missing and if the field is already marked as being an id, a cas or a join, the value of this property is
false
. - The
true
value is equivalent to{}
. - The schema of the content of this custom property is available here.
This property is responsible for generating the Field
annotation.
E.g., this schema:
{
"..."
"properties": {
"..."
"field": {
"title": "A field",
"type": "string"
},
"..."
}
}
Will produce:
@Field
private String field;
Some sub-properties are available to manage the annotation parameters (detailed in the annotation's documentation).
E.g., this schema:
{
"..."
"properties": {
"..."
"field": {
"title": "A field",
"type": "string",
"x-cb-field": {
"name": "field_",
"order": 5
}
},
"..."
}
}
Will produce:
@Field(name = "field_", order = 5)
private String field;
Use field for id generation
An idPrefix
sub-property is available to manage the the IdAttribute
annotation and its parameters (detailed in the annotation's documentation).
E.g., this schema:
{
"..."
"properties": {
"..."
"field": {
"title": "A field",
"type": "string",
"x-cb-field": {
"idAttribute": true
}
},
"..."
}
}
Will produce:
@Field
@IdAttribute
private String field;
And this schema:
{
"..."
"properties": {
"..."
"field": {
"title": "A field",
"type": "string",
"x-cb-field": {
"idAttribute": {
"order": 2
}
}
},
"..."
}
}
Will produce:
@Field
@IdAttribute(order = 2)
private String field;
You can find more information on Spring Data Couchbase key generation here.
Indexes
A JSON sub-property index
is available to declare indexes that will can be auto-generated by Spring Data Couchbase via the QueryIndexed
annotation and its parameters (detailed in the annotation's documentation).
E.g., this schema:
{
"..."
"properties": {
"..."
"field": {
"title": "A field",
"type": "string",
"x-cb-field": {
"index": true
}
},
"..."
}
}
Will produce:
@Field
@QueryIndexed
private String field;
And this schema:
{
"..."
"properties": {
"..."
"field": {
"title": "A field",
"type": "string",
"x-cb-field": {
"index": {
"direction": "ASCENDING",
"name": "idx_sample"
}
}
},
"..."
}
}
Will produce:
@Field
@QueryIndexed(direction = QueryIndexDirection.ASCENDING, name = "idx_sample")
private String field;
You can find more information on Spring Data Couchbase index creation and how to activate it here here.
Non persisted fields for key generation
At property level, two JSON properties are available to use fields in code for key generation without persisting them, via the following annotations and their parameters (detailed in the annotation's documentation).
E.g., this schema:
{
"..."
"properties": {
"..."
"prefix": {
"title": "A field",
"type": "string",
"x-cb-idPrefix": true
},
"..."
}
}
Will produce:
@IdPrefix
private String prefix;
And this schema:
{
"..."
"properties": {
"..."
"suffix": {
"title": "A field",
"type": "string",
"x-cb-idSuffix": {
"order": 1
}
},
"..."
}
}
Will produce:
@Field
@IdSuffix(order = 1)
private String suffix;
You can find more information on Spring Data Couchbase key generation here.
Maven configuration
Here is an example of how the extension can be added to the jsonschema2pojo Maven plugin.
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>${jsonschema2pojo.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
...
<!-- Extension RuleFactory -->
<customRuleFactory>
dev.hctbst.jsonschema2pojo.springframework.data.couchbase.SpringDataCouchbaseRuleFactory
</customRuleFactory>
</configuration>
</execution>
</executions>
<dependencies>
<!-- Extension dependency -->
<dependency>
<groupId>dev.hctbst</groupId>
<artifactId>jsonschema2pojo-spring-data-couchbase</artifactId>
<version>${jsonschema2pojo-spring-data-couchbase.version}</version>
</dependency>
<!-- Spring Data Couchbase dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-couchbase</artifactId>
<version>${spring-boot.version}</version>
</dependency>
</dependencies>
</plugin>
A more complete example is available here.
License
This project is released under version 2.0 of the Apache License.