Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce specialized expression types for easier JPA Criteria querying #519

Open
beikov opened this issue Aug 31, 2023 · 2 comments · May be fixed by #522
Open

Introduce specialized expression types for easier JPA Criteria querying #519

beikov opened this issue Aug 31, 2023 · 2 comments · May be fixed by #522

Comments

@beikov
Copy link

beikov commented Aug 31, 2023

I don't know how feasible this is, but this idea just came to mind and I wanted to share it here.

What if we introduced special Attribute, Expression and Path types for certain known types to make it easier to construct predicates and/or functions. I propose the following additions:

  • NumberAttribute, NumberExpression and NumberPath when the type extends Number
  • StringAttribute, StringExpression and StringPath when the type extends String
  • TemporalAttribute, TemporalExpression and TemporalPath when the type extends Temporal
  • ComparableAttribute, ComparableExpression and ComparablePath when the type extends Comparable

Then we add some overloads to Path:

NumberPath<Y> get(NumberAttribute<? super X, Y> attribute);
...

And all of a sudden we could write nicer queries that don't involve calling methods on the CriteriaBuilder e.g.:

cq.where( root.get(MyType_.price).plus(1).gt(10) )

instead of

cq.where( cb.gt( cb.sum( root.get(MyType_.price), 1), 10) )

Of course this is just a simple example, but I think that the major criticism against the JPA Criteria API is that it is hard to write and read, which I think is mostly due to the need of involving the CriteriaBuilder everywhere.

I know that there is a small "type explosion" here i.e. 3 classes for every type we want to add decent support for, but I think it's worth it and that the number of supported types will stay in the single digit realm :)

@gavinking
Copy link
Contributor

That's a good idea @beikov.

How about prototyping your proposal as a pull req?

@AleksNo
Copy link

AleksNo commented Aug 31, 2023

Hello,

i like the idea. :)

Cheers

beikov added a commit to beikov/persistence that referenced this issue Sep 1, 2023
beikov added a commit to beikov/persistence that referenced this issue Sep 1, 2023
beikov added a commit to beikov/persistence that referenced this issue Sep 19, 2023
beikov added a commit to beikov/persistence that referenced this issue Sep 19, 2023
beikov added a commit to beikov/persistence that referenced this issue Sep 19, 2023
beikov added a commit to beikov/persistence that referenced this issue Sep 19, 2023
beikov added a commit to beikov/persistence that referenced this issue Sep 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants