More Types
In addition to boolean, number, string, UUID, date, and enumeration, scalar properties also support other types, including:
- Array types
- JSON types
- Custom types
Enum types are not discussed in this article, please refer to Enum Mapping
Array Types
To use array types, the underlying database must support array types
- Java
- Kotlin
@Entity
public interface Book {
@Id
long id();
String[] tags();
}
@Entity
interface Book {
@Id
val id: Long
val tags: Array<String>
}
For Postgres, you need to specify the data element type in SQL, for example:
- Java
- Kotlin
@Entity
public interface Book {
@Id
long id();
@Column(sqlElementType = "text")
String[] tags();
}
@Entity
interface Book {
@Id
val id: Long
@Column(sqlElementType = "text")
val tags: Array<String>
}
JSON Types
You can use Jackson to support scalar properties of any type, whether it's a custom Java/Kotlin type, a collection type, or even a mix of the two.
Just use the @org.babyfish.jimmer.sql.Serialized
annotation, and you can use the JSON type.
Here, the usage is demonstrated using a collection type as an example:
- Java
- Kotlin
@Entity
public interface Book {
@Id
long id();
@Serialized
Map<String, Map<String, List<Integer>>> data();
}
@Entity
interface Book {
@Id
val id: Long
@Serialized
val data: Map<String, Map<String, List<Integer>>>
}
So what SQL type does the JSON type correspond to?
-
If the database supports the JSON or JSONB type, use that type
-
Otherwise, please use the string type
For Postgres, it supports operations on the internal structure of JSON, please see JSON Operations in Postgres. How should Jimmer implement such operations?
Jimmer's SQL DSL can mix in Native SQL expressions, please see Native Expressions, this article will not repeat it. Note:
JSON Operations in Postgres use ?
, which happens to be the parameter for JDBC, please use ??
instead.
Custom Types
If the JSON type still cannot meet your requirements (e.g., you expect to map some Postgres-specific types), you can use ScalarProvider
to customize types.
Please refer to ScalarProvider, this article will not repeat it.