Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sprint JPA Close Projection Interface always return null #3230

Closed
Sirikakire opened this issue Nov 17, 2023 · 2 comments
Closed

Sprint JPA Close Projection Interface always return null #3230

Sirikakire opened this issue Nov 17, 2023 · 2 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@Sirikakire
Copy link

Sirikakire commented Nov 17, 2023

Can somebody help me this case...I try very hard to find out the solutions but nothing work!! For a personal reason, i have to use Native query

 

@Query(value = 
    "SELECT  " +
    "   p.Product_ID as productId, " + 
    "   p.Product_Name as productName, " + 
    "   p.Product_Description as productDescription, " + 
    "   p.Product_Is_Popular as productIsPopular, " +
    "   p.Product_Active as productActive, " + 
    "   p.Product_Creation_Date as productCreationDate, " + 
    "   p.Product_Image_Url as productImageUrl, " + 
    "   MIN(ps.Product_Size_Price) as productMinPrice, " +
    "   c.Category_ID, " + 
    "   c.Category_Name " + 
    "FROM Product p " +
    "INNER JOIN Product_Size ps ON ps.Product_ID = p.Product_ID " +
    "INNER JOIN Category c ON c.Category_ID = p.Category_ID " +
    "GROUP BY  " +
    "   p.Product_ID, " +
    "   p.Product_Name, " +
    "   p.Product_Active, " +
    "   p.Product_Creation_Date, " +
    "   p.Product_Image_Url, " +
    "   p.Product_Is_Popular, " +
    "   p.Product_Description, " +
    "   c.Category_ID, " +
    "   c.Category_Name"
, nativeQuery = true)
	List<ProductEntityWithMinPrice> findAllProductWithMinPrice();
package com.duan.entity;
import java.math.BigDecimal;
import java.util.Date;

public interface ProductEntityWithMinPrice{
  Integer getProductId();
  String getProductName();
  String getProductDescription();
  Boolean getProductIsPopular();
  Boolean getProductActive();
  Date getProductCreationDate();
  String getProductImageUrl();
  BigDecimal getProductMinPrice();
  Category getCategory();
  interface Category{
    Integer getCategory_Id();    
    String getCategory_Name();
  }
}

RESULT:

{
    "productId": 1,
    "productImageUrl": "somthing.png",
    "productName": "name1",
    "productDescription": "nothing.",
    "productCreationDate": 1699612643976,
    "productIsPopular": false,
    "productActive": true,
    "productMinPrice": 18000,
    "category": null     <---------- Always return null
},
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 17, 2023
@mp911de
Copy link
Member

mp911de commented Nov 20, 2023

Category_ID and Category_Name are columns on the same level as for Product_Is_Popular. Since this is a SQL query that uses a JOIN (and not a JPQL query), you will have to inline Category into ProductEntityWithMinPrice.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Nov 20, 2023
@mp911de mp911de added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 20, 2023
@Sirikakire
Copy link
Author

Sirikakire commented Nov 25, 2023

Category_ID and Category_Name are columns on the same level as for Product_Is_Popular. Since this is a SQL query that uses a JOIN (and not a JPQL query), you will have to inline Category into ProductEntityWithMinPrice.

Aren't there any way to create Category object?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants