数组类型

我们可以使用 @DbArray 映射一组或一列字符串、UUID、整数或长整数。

@Entity
public class Customer {

  @Id
  private long id;

  private String name;

  @DbArray
  private Set<String> tags = new LinkedHashSet<>();

  // getters and setters
}
  
@Entity
class Customer(name: String) {

  @Id
  var id: Long = 0

  var name: String = name

  @DbArray
  var tags = emptyMutableSet<>();

}
  

数组类型的 DDL

在上述示例中,tags 属性映射到 varchar[]

create table customer (
  id                            bigserial not null,
  name                          varchar(255) not null,
  registered                    date,
  tags                          varchar[],
  version                       bigint not null,
  when_created                  timestamptz not null,
  when_modified                 timestamptz not null,
  constraint pk_customer primary key (id)
);

数组包含表达式

我们通常希望使用 contains 表达式。

final List<Customer> customers
  = new QCustomer()
  .tags.contains("BLUE")
  .findList();
val customers
  = QCustomer()
  .tags.contains("BLUE")
  .findList()

上述查询的 SQL 是

select t0.id, t0.name, t0.registered, t0.tags, t0.version, t0.when_created, t0.when_modified
  from customer t0
 where t0.tags @> array[?]; --bind(BLUE)