Array type
We can use @DbArray
to map a Set or List of String, UUID, Integer or Long.
@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 for Array types
In the above example the tags property maps to a 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)
);
Array contains expression
Commonly we want to use the contains expression.
final List<Customer> customers
= new QCustomer()
.tags.contains("BLUE")
.findList();
val customers
= QCustomer()
.tags.contains("BLUE")
.findList()
The SQL for the above query is:
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)