Skip to main content

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
info

Enum types are not discussed in this article, please refer to Enum Mapping

Array Types

warning

To use array types, the underlying database must support array types

@Entity
public interface Book {

@Id
long id();

String[] tags();
}

For Postgres, you need to specify the data element type in SQL, for example:

@Entity
public interface Book {

@Id
long id();

@Column(sqlElementType = "text")
String[] tags();
}

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:

@Entity
public interface Book {

@Id
long id();

@Serialized
Map<String, Map<String, List<Integer>>> data();
}

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:

caution

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.