Skip to content

Conversation

@gavinking
Copy link
Member

@gavinking gavinking commented Apr 18, 2025

Usage looks like:

var books = 
        SelectionSpecification.create(Book.class)
                 .addRestriction(Restriction.like(Book_.title, "%Hibernate%", false))
                 .addRestriction(Restriction.greaterThan(Book_.pages, 100))
                 .addFetching(Path.from(Book.class).to(Book_.authors))
                 .setOrder(Order.desc(Book_.title))
                 .createQuery(session)
                 .getResultList();

Looks just like ye olde Hibernate criteria, but perfectly type safe!

Also now allowed:

var books = 
	SelectionSpecification.create(Book.class)
	          .addAugmentation((builder, query, book) -> 
	                  query.where(builder.like(book.get(Book_.title), "%Hibernate%")))
                  .addFetching(Path.from(Book.class).to(Book_.authors))
	          .addOrdering(Order.asc(Book_.title))
	          .createQuery(session)
	          .getResultList();

That is .... actually really nice.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.


https://hibernate.atlassian.net/browse/HHH-19364

@gavinking gavinking force-pushed the HHH-19364-fully-detached branch from 3331682 to 90a6564 Compare April 18, 2025 09:44
@gavinking
Copy link
Member Author

gavinking commented Apr 18, 2025

This even works rather nicely with CriteriaDefinition:

SelectionSpecification.create( BasicEntity.class )
	.addAugmentation((builder, query, entity) ->
	    new CriteriaDefinition<>(query) {{
	        where(like(entity.get(BasicEntity_.name), "%" ),
		      equal(entity.get(BasicEntity_.position), 1));
	    }}
	)
	.createQuery(session)
	.getResultList();

That's probably better than plain vanilla usage of CriteriaDefinition.

@gavinking gavinking merged commit 646b876 into hibernate:main Apr 18, 2025
20 of 23 checks passed
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 this pull request may close these issues.

1 participant