Skip to content

Instantly share code, notes, and snippets.

@joedayz
Last active February 3, 2022 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joedayz/01b6211964c1e868ef457656858da599 to your computer and use it in GitHub Desktop.
Save joedayz/01b6211964c1e868ef457656858da599 to your computer and use it in GitHub Desktop.
postgresql-array-java-list
**************************************************Entity*****************************************
@Entity(name = "loanApplicationInstance")
@Table(name = "tenant_loan_application_instances")
@ToString
@TypeDefs({
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class),
@TypeDef(name = "list-array", typeClass = ListArrayType.class)
})
public class TenantLoanApplicationInstance extends TenantAudit
...
@Type(type = "list-array")
@Column(name = "participants", columnDefinition = "text[]")
private List<String> participants;
....
**************************************************Repository.findAll*****************************************
filter.addCondition(
Condition.builder()
.type(FilterFieldTypes.ARRAY)
.comparison(FilterComparison.IN)
.field("participants")
.value(principal.getName())
.build());
data = tenantLoanApplicationInstanceRepository.findAll(filter, pageRequest);
**************************************************Filter******************************************************
public class Filter<T> implements Specification<T> {
private final List<Condition> conditions = new ArrayList<>();
public void addCondition(Condition condition) {
this.conditions.add(condition);
}
private List<Predicate> buildPredicates(Root root, CriteriaQuery query, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<>();
for (Condition condition : conditions) {
predicates.add(buildPredicate(condition, root, query, cb));
}
return predicates;
}
public Predicate buildPredicate(
Condition condition, Root root, CriteriaQuery query, CriteriaBuilder cb) {
if (!condition.getType().getValue().contains(condition.getComparison())) {
throw new RuntimeException("UNSUPPORTED_COMPARISON_FOR_SPECIFIED_TYPE");
}
switch (condition.getComparison()) {
case EQ:
if (condition.getValue().getClass().isArray()) {
final Predicate[] predicatesList =
Arrays.stream((Object[]) condition.getValue())
.map(conditionValue -> cb.equal(root.get(condition.getField()), conditionValue))
.toArray(Predicate[]::new);
return cb.or(predicatesList);
}
return cb.equal(root.get(condition.getField()), condition.getValue());
case GT:
if (condition.getType().equals(FilterFieldTypes.DATE)) {
return cb.greaterThan(
root.<Date>get(condition.getField()),
new Date(condition.getValueAsNumber().longValue()));
} else {
return cb.gt(root.get(condition.getField()), condition.getValueAsNumber());
}
case GTE:
if (condition.getType().equals(FilterFieldTypes.DATE)) {
return cb.greaterThanOrEqualTo(
root.<Date>get(condition.getField()),
new Date(condition.getValueAsNumber().longValue()));
} else {
return cb.ge(root.get(condition.getField()), condition.getValueAsNumber());
}
case LT:
if (condition.getType().equals(FilterFieldTypes.DATE)) {
return cb.lessThan(
root.<Date>get(condition.getField()),
new Date(condition.getValueAsNumber().longValue()));
} else {
return cb.lt(root.get(condition.getField()), condition.getValueAsNumber());
}
case LTE:
if (condition.getType().equals(FilterFieldTypes.DATE)) {
return cb.lessThanOrEqualTo(
root.<Date>get(condition.getField()),
new Date(condition.getValueAsNumber().longValue()));
} else {
return cb.le(root.get(condition.getField()), condition.getValueAsNumber());
}
case FULL_TEXT:
return cb.isTrue(cb.function("fts", Boolean.class, cb.literal(condition.getValue())));
case JSONB_ARRAY_IN:
return cb.isTrue(
cb.function(
"jsonb_extract_path_text", Boolean.class, cb.literal(condition.getValue())));
case JSONB_PATH_EQ:
return cb.isTrue(
cb.function(
"jsonb_array_contains",
Boolean.class,
cb.literal(condition.getField()),
cb.literal(condition.getValue())));
case IN:
List<String> dataList = Arrays.asList(condition.getValue().toString());
return cb.in(root.get(condition.getField())).value(dataList);
default:
throw new RuntimeException("UNSUPPORTED_COMPARISON");
}
}
public int getConditionsCount() {
return conditions.size();
}
@Override
public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
List<Predicate> predicates = buildPredicates(root, query, cb);
return predicates.size() > 1 ? cb.and(predicates.toArray(Predicate[]::new)) : predicates.get(0);
}
}
*******************Error in it class*************
public class ListArrayTypeDescriptor extends AbstractArrayTypeDescriptor<Object> {
...
@Override
public Object unwrap(Object value, Class type, WrapperOptions options) {
if (value instanceof Object[]) {
return value;
} else if (value instanceof List) {
return super.unwrap(((List) value).toArray(), type, options);
} else {
throw new UnsupportedOperationException("The provided " + value + " is not a Object[] or List!");
}
}
...
*****************This is my Error*******************
java.lang.UnsupportedOperationException: The provided d08d7785-1153-4bb3-b58c-6c79bf5a37f8 is not a Object[] or List!
at com.vladmihalcea.hibernate.type.array.internal.ListArrayTypeDescriptor.unwrap(ListArrayTypeDescriptor.java:60)
at
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment