'Handling Regular Expressions with JPA Criteria API
Is it possible to implement Pattern querying with JPA Criteria API?
In my case, regex patterns are stored into a quick bag property; and I'm trying to avoid using native queries (e.g. PostgreSQL POSIX support).
@Entity @Table(name = "rcp")
public class Recipient {
@Id @Column(name = "rcp_email_id")
@Email
private String email;
@CollectionTable(joinColumns = @JoinColumn(name = "rcp_email_fk"))
@ElementCollection(fetch = EAGER)
@Convert(converter = PatternConverter.class)
private Set<Pattern> rules = new HashSet<>();
...
}
So I figured I could use the Criteria API but failed to properly develop the technlogy, obviously:
@AllArgsConstructor
public class RecipientSpecification implements Specification<Recipient> {
private String sample;
@Override
public Predicate toPredicate(Root<Recipient> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
return builder.exists(root.join("rules").as(Pattern.class).matcher(sample).find());
}
}
I thought I could work on the join with a cast and execute the Java Pattern logic by casting the properties, which I realize to be dumb now because it has no sense from the JPA DSL point of view. It doesn't even compile! But is there a proper way to proceed?
Thanks in advance
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
