How to use regular expressions in Criteria Builder

#1

I'm struggling to understand how to use the matches method on the String class when using regular expressions.

I have a small example with two classes. The TempPhysicalPartDetails class is embedded in the TempPhysicalPart class.

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TempPhysicalPart {
    private Long id;

    private TempPhysicalPartDetails physicalPartDetails;
}

 

@Embeddable
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TempPhysicalPartDetails {
    String partNumber;

    String serialNumber;
}

 

I am trying to compare the partNumber and serialNumber against the user provided regex expression.

For example, if a user wanted all part numbers ending in 802. They would pass the expression *802. 

I just can't figure out where this would be in the criteria builder.

{
    CriteriaBuilder cb = emf.getCriteriaBuilder();
    CriteriaQuery<TempPhysicalPart> q = cb.createQuery(TempPhysicalPart.class);

    Root<TempPhysicalPart> c = q.from(TempPhysicalPart.class);

    q.select(c).where(cb.equal(c.get("details").get("partNumber"), "82382"));

    q.select(c).where(cb.equal(c.get("details").get("serialNumber"), "00012"));

}

 

The EntityManagerFactory implementation is excluded from.

#2

JPA does not support regular expressions in queries, so currently you cannot use regular expressions in JPA criteria API (you can use LIKE, which is less powerful). ObjectDB supports regular expressions in queries as an extension, but that extension is limited to string-based JPQL queries, as the criterial API is not extendible.

ObjectDB Support
#3

Bummer. Is this BIRT/ODA ObjectDB Driver the extension that your referring to?

#4

No. This extension is part of ObjectDB so you can use regular expressions in queries, just use string-based JPQL ("e.g. e.field.matches(regexp)") instead of criteria queries.

ObjectDB Support

Reply