In order to use JSONB in CUBA, you’ll need to create custom data converters and specify them in persistence.xml. Also, you’ll need to change an entity’s column definition to store JSONB.
This example application stores a person in a DB table and their address as JSON.
Please note that you won’t be able to search entities that are stored in JSON format, but you should be able to edit data.
Key moments:
com.company.jsonb.entity.JsonConverter
class that converts JSON to Address entity.- Person's attribute definition:
@Convert(converter = JsonConverter.class)
@Column(name = "ADDRESS", columnDefinition = "jsonb")
private Address address;
- View definition in both browser and editor screens. Please note fetch type:
<view extends="_local">
<property name="address" fetch="UNDEFINED" view="_minimal"/>
</view>
- Persistence unit definition:
<persistence-unit name="jsonb-test" transaction-type="RESOURCE_LOCAL">
<class>com.company.jsonb.entity.JsonConverter</class>
<class>com.company.jsonb.entity.Person</class>
</persistence-unit>
- PostgreSQL’s driver should be specified as a dependency in global module:
dependencies {
if (!JavaVersion.current().isJava8()) {
runtime('javax.xml.bind:jaxb-api:2.3.1')
runtime('org.glassfish.jaxb:jaxb-runtime:2.3.1')
}
compile(postgres);
}