• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Znak _ underscore w zapytaniach w Spring Data

Object Storage Arubacloud
0 głosów
614 wizyt
pytanie zadane 29 września 2019 w Java przez heartagram Obywatel (1,770 p.)

Cześć,

Spotkałem się z sytuacją w której zapytanie do bazy w Spring Repository przez Postmana z endpointu REST wyglądało i działało tak:

@Repository
public interface ToDoListRepository extends JpaRepository<ToDoList, Long> {

	ToDoList getByIdAndToDoListOwner_Username(Long id, String username);
}

ja chciałem to zrobić w swojej klasie przez :

ToDoList getByIdAndToDoListOwner(Long id, String toDoListOwner);

i otrzymałem błąd:

    "message": "Parameter value [user123] did not match expected type [pl.test.user.User (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [user123] did not match expected type [pl.test.user.User (n/a)]",

 

Nie rozumiem dlaczego pierwszy sposób działa, a mój nie. 

2 odpowiedzi

+1 głos
odpowiedź 1 października 2019 przez mbabane Szeryf (79,280 p.)

Skoreluj to z tym:

https://docs.spring.io/spring-data/commons/docs/2.2.0.RC3/reference/html/#repositories.query-methods.query-property-expressions

Property expressions can refer only to a direct property of the managed entity, as shown in the preceding example. At query creation time, you already make sure that the parsed property is a property of the managed domain class. However, you can also define constraints by traversing nested properties. Consider the following method signature:

List<Person> findByAddressZipCode(ZipCode zipCode);

Assume a Person has an Address with a ZipCode. In that case, the method creates the property traversal x.address.zipCode. The resolution algorithm starts by interpreting the entire part (AddressZipCode) as the property and checks the domain class for a property with that name (uncapitalized). If the algorithm succeeds, it uses that property. If not, the algorithm splits up the source at the camel case parts from the right side into a head and a tail and tries to find the corresponding property — in our example, AddressZip and Code. If the algorithm finds a property with that head, it takes the tail and continues building the tree down from there, splitting the tail up in the way just described. If the first split does not match, the algorithm moves the split point to the left (Address, ZipCode) and continues.

Although this should work for most cases, it is possible for the algorithm to select the wrong property. Suppose the Person class has an addressZip property as well. The algorithm would match in the first split round already, choose the wrong property, and fail (as the type of addressZip probably has no code property).

To resolve this ambiguity you can use _ inside your method name to manually define traversal points. So our method name would be as follows:

List<Person> findByAddress_ZipCode(ZipCode zipCode);

Because we treat the underscore character as a reserved character, we strongly advise following standard Java naming conventions (that is, not using underscores in property names but using camel case instead).

0 głosów
odpowiedź 30 września 2019 przez Arkadiusz Fajdek Dyskutant (9,450 p.)

To co mówi bład to że do klasy User (pl.test.user.User) prubujesz przeslać Stringa.
Musiał bym zerknać na kod żeby zobaczyć co się tam dzieje ale zdaje się że po prostu typy się nie zgadzają.

ToDoList getByIdAndToDoListOwner(Long id, User toDoListOwner);

I zrób sobie mapowanie tego stringa na klase User

Podobne pytania

0 głosów
0 odpowiedzi 258 wizyt
pytanie zadane 18 marca 2020 w Java przez Krab789 Nowicjusz (170 p.)
0 głosów
2 odpowiedzi 264 wizyt
0 głosów
1 odpowiedź 202 wizyt
pytanie zadane 27 kwietnia 2017 w Java przez Jonki Dyskutant (8,180 p.)

92,556 zapytań

141,404 odpowiedzi

319,563 komentarzy

61,942 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Kolejna edycja największej imprezy hakerskiej w Polsce, czyli Mega Sekurak Hacking Party odbędzie się już 20 maja 2024r. Z tej okazji mamy dla Was kod: pasjamshp - jeżeli wpiszecie go w koszyku, to wówczas otrzymacie 40% zniżki na bilet w wersji standard!

Więcej informacji na temat imprezy znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...