ARTICLE AD BOX
I have 3 relevant classes
@Entity @Table @IdClass(SzczegolyZamowieniaId.class) public class SzczegolyZamowienia { int ilosc; @Id @ManyToOne Zamowienie zamowienie; @Id @ManyToOne Produkt produkt; //getters/setters/constructors } @Entity @Table public class Zamowienie { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) int id; LocalDate data; @ManyToOne Klient klient; //getters/setters/constructors } @Entity @Table public class Produkt { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public int id; String nazwa; String opis; int cena; //getters/setters/constructors }and a projection interface
public interface TopProdukt { public int getIlosc(); public String getNazwa(); public String getOpis(); public int getCena(); }and I'm trying to run query like this:
@Query("select sum(sz.ilosc) as Ilosc,sz.produkt.nazwa as Nazwa,sz.produkt.opis as Opis,sz.produkt.cena as Cena from SzczegolyZamowienia sz group by sz.produkt order by sum(sz.ilosc) desc") public Collection<TopProdukt> topProdukt();but it doesn't work
There was an unexpected error (type=Internal Server Error, status=500). Could not write JSON: Null return value from advice does not match primitive return type for: public abstract int org.example.serwisy.projekcja.TopProdukt.getCena() org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Null return value from advice does not match primitive return type for: public abstract int org.example.serwisy.projekcja.TopProdukt.getCena()but on the other hand native query like this
@Query(value = """ select sum(sz.ilosc) as Ilosc,p.nazwa as Nazwa,p.opis as Opis,p.cena as Cena from Szczegoly_Zamowienia sz INNER JOIN Zamowienie z ON z.id=sz.zamowienie_id INNER JOIN Produkt p ON p.id=sz.produkt_id group by p.id order by sum(sz.ilosc) desc """,nativeQuery = true) public Collection<TopProdukt> topProdukt();works without any issue. For me these should do the same thing. Am I wrong?
Same database, same data, springboot 4.0.5
