All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ca.gc.aafc.dina.jpa.JsonbValueSpecification Maven / Gradle / Ivy

The newest version!
package ca.gc.aafc.dina.jpa;

import lombok.RequiredArgsConstructor;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 * Allows to create JPA Predicate matching a value on a specific jsonb key.
 * PostgreSQL specific.
 * @param 
 */
@RequiredArgsConstructor
public class JsonbValueSpecification {

  static final String JSONB_EXTRACT_PATH_PG_FUNCTION_NAME = "jsonb_extract_path_text";

  private final String columnName;
  private final String keyName;

  public Predicate toPredicate(Root root, CriteriaBuilder builder, String value) {
    return builder.equal(builder
        .function(JSONB_EXTRACT_PATH_PG_FUNCTION_NAME,
            String.class,
            root.get(this.columnName),
            builder.literal(this.keyName)),
        value);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy