Coder Social home page Coder Social logo

Comments (10)

spring-projects-issues avatar spring-projects-issues commented on May 15, 2024

Nemanja Nedic commented

Have you considered adding an implementation that uses JPA Path interface or something equivalent? This is important in cases where we have a parent-child relationship and want to sort by parent field while selecting child entities

from spring-data-jpa.

spring-projects-issues avatar spring-projects-issues commented on May 15, 2024

Oliver Drotbohm commented

This is what JPA Metamodel actually means :). What do you exactly mean with parent? If the parent object is reachable from the child object then a new Sort("parent.foo", Direction.ASC) should do the trick with the current String-based implementation

from spring-data-jpa.

spring-projects-issues avatar spring-projects-issues commented on May 15, 2024

Stevo Slavić commented

It doesn't work and at the moment we believe issue is in hades/spring-data-jpa ( http://redmine.synyx.org/boards/2/topics/221 ) and not JPA implementation we're using (Hibernate). We think that hades/spring-data-jpa should do additional processing when creating queries with sort using dot notation. We'll try to create a simple reproducible example/test

from spring-data-jpa.

spring-projects-issues avatar spring-projects-issues commented on May 15, 2024

Nemanja Nedic commented

I would recommend to use additional parsing in the toJpaOrder method of QueryUtils class. Something like this:

private static javax.persistence.criteria.Order toJpaOrder(Order order, Root<?> root, CriteriaBuilder cb) {
	Expression<?> expression = null;
	String pathString = order.getProperty();
	String[] pathElements = pathString.split(".");
	int pathSize = pathElements.length;

	if (pathSize > 1) {
		Join<Object, Object> path = root.join(pathElements[0]);
		for (int i = 1; i < pathSize - 1; i++) {
			path = path.join(pathElements[0]);
		}
		expression = path.get(pathElements[pathSize - 1]);
	} else {
		expression = root.get(pathString);
	}
	return order.isAscending() ? cb.asc(expression) : cb.desc(expression);
}

from spring-data-jpa.

spring-projects-issues avatar spring-projects-issues commented on May 15, 2024

Nemanja Nedic commented

There are some bugs in my example above. This one is tested and works for us:

private static javax.persistence.criteria.Order toJpaOrder(Order order,
		Root<?> root, CriteriaBuilder cb) {
	Expression<?> expression = null;
	String pathString = order.getProperty();
	String[] pathElements = pathString.split("\\.");
	int pathSize = pathElements.length;

	if (pathSize > 1) {
		Join<Object, Object> path = root.join(pathElements[0]);
		for (int i = 1; i < pathSize - 1; i++) {
			path = path.join(pathElements[i]);
		}
		expression = path.get(pathElements[pathSize - 1]);
	} else {
		expression = root.get(pathString);
	}
	return order.isAscending() ? cb.asc(expression) : cb.desc(expression);
}

from spring-data-jpa.

spring-projects-issues avatar spring-projects-issues commented on May 15, 2024

Oliver Drotbohm commented

Okay, it seems we were talking about different things here. This ticket is more about a special Sort implementation that takes Path parameters instead of String instances as this would allow usage of generated metamodel classes for sort expressions.

I'll be applying your patch nonetheless as it seems to be a more accurate interpretation of the spec than the current version

from spring-data-jpa.

spring-projects-issues avatar spring-projects-issues commented on May 15, 2024

Stevo Slavić commented

Note: Patch was not tested (yet) when path element references @Embedded @Embeddable property

from spring-data-jpa.

spring-projects-issues avatar spring-projects-issues commented on May 15, 2024

Oliver Drotbohm commented

Consolidated implementation derived from that ticket with some code we already had in JpaQueryCreator in course of fixing DATAJPA-103

from spring-data-jpa.

spring-projects-issues avatar spring-projects-issues commented on May 15, 2024

Thomas Darimont commented

Added PR: #54

from spring-data-jpa.

spring-projects-issues avatar spring-projects-issues commented on May 15, 2024

Thomas Darimont commented

Please revise

from spring-data-jpa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.