Giter Site home page Giter Site logo

Comments (5)

jradlica avatar jradlica commented on August 17, 2024 1

The minimal code which reproduces the bug:

(Issue138.java - it should be placed in /src/test/java/...)

import net.kaczmarzyk.E2eTestBase;
import net.kaczmarzyk.spring.data.jpa.Customer;
import net.kaczmarzyk.spring.data.jpa.CustomerRepository;
import net.kaczmarzyk.spring.data.jpa.domain.In;
import net.kaczmarzyk.spring.data.jpa.web.annotation.JoinFetch;
import net.kaczmarzyk.spring.data.jpa.web.annotation.Spec;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class Issue138 extends E2eTestBase {

	@RestController
	public static class TestControllerIssue138 {

		@Autowired
		private CustomerRepository customerRepository;

		@RequestMapping(value = "/issue138/join-fetch-interface/customers", params = { "orderIn" })
		@ResponseBody
		public Object findByOrderIn(@JoinFetch(paths = "orders", alias = "o")
									@Spec(path = "o.itemName", params = "orderIn", spec = In.class) Specification<Customer> spec, Pageable pageable) {
			return customerRepository.findAll(spec, pageable);
		}

	}

	@Test
	public void resolveSpecBasedOnJoinFetchAliasForPagedRequest() throws Exception {
		mockMvc.perform(get("/issue138/join-fetch-interface/customers")
				.param("orderIn", "Pizza")
						.param("order", "Duff Beer")
						.param("page", "0")
						.param("size", "1")
						.param("sort", "id")
				.accept(MediaType.APPLICATION_JSON))
			.andExpect(status().isOk())
			.andExpect(jsonPath("$").isArray())
			.andExpect(jsonPath("$[0].firstName").value("Homer"))
			.andExpect(jsonPath("$[1]").doesNotExist());
	}

}

from specification-arg-resolver.

tkaczmarzyk avatar tkaczmarzyk commented on August 17, 2024

@yarikwest Thanks for reporting. After short analysis I understand the issue and prepared a working PoC of a fix. But it needs more testing and refactoring.

In a nuthshell:

  1. JoinFetch had an explicit skip of joinin in count queries -- this was done for 2 reasons: 1) fetch is very often used for just initiualizing lazy collections, not filtering so it would be superfluous for count query 2) hibernate seems to forbid join fetch in count query (i.e. when root entity is not included in the result list)
  2. I added a quick hackish fragment that in case of a count query converts join fetch to a join...
  3. ... and the test posted by @jradlica seems to be working fine -- see related PR

I think this is the way to go, but:

  1. More tests must be added (e.g. for multi-level joins)
  2. JoinFetch to Join conversion must be skipped if there is no actual filtering on the joined entity (in such a case it would be superfluous in a count query)
  3. JoinFetch allows multiple paths in a single annoatation, in the PoC I used just the first one

So it requires more work. You can compiling the version from the branch and see if it solves your issue in the meantime

from specification-arg-resolver.

tkaczmarzyk avatar tkaczmarzyk commented on August 17, 2024

(Marking as good first issue because sb could at least write more tests e.g. with nested joins (i.e. join fetching on a joined entity). The actual implementation requires more deep knowledge of the internals)

from specification-arg-resolver.

tkaczmarzyk avatar tkaczmarzyk commented on August 17, 2024

(uploaded as 2.9.1-SNAPSHOT to my private Maven repo for easier evaluation, see bottom of the README for the location details)

from specification-arg-resolver.

tkaczmarzyk avatar tkaczmarzyk commented on August 17, 2024

@yarikwest fix included in v2.10.0 (just released)

from specification-arg-resolver.

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.