1

I have a native query in JPA

String sql1 = """
                       SELECT tier_id, tier_list, vm.id AS model_id, option_list, price, sku, stock
                       FROM (
                           SELECT *
                           FROM ecommerce.tier_variations tv
                           WHERE tv.product_id = ?1 AND is_deleted = false
                       ) AS tv
                       JOIN ecommerce.variation_models vm ON vm.tier_id = tv.id
              """;
@Query(value = sql1, nativeQuery = true)
List<VariationProjection> findVariations(String productId);

The result of option_list is [{"image": "image1", "option": "L"}, {"image": "image2", "option": "X"}] in db

I use projection to map

public interface VariationProjection {
    List<String> getTier_list();
    List<Map<String, Object>> getOption_list();
}

List getTier_list() is work, but List<Map<String, Object>> getOption_list() not work Please help me this problem T_T

3
  • Don't use images for text. Copy code and logs into the question as text using code block format. Commented Dec 8, 2024 at 4:25
  • i have fix images, do you know this problem T_T Commented Dec 8, 2024 at 8:11
  • Would you share the part of result (native query)? Commented Dec 8, 2024 at 13:52

1 Answer 1

1

Cause

  • JPA does not support automatic JSON mapping: The native query returns the option_list value as a JSON getOption_list, and JPA doesn't know how to convert this value into a List<Map<String, Object>>.

Solution using Jackson

Add the Jackson dependency to your project:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>

Create a custom converter:

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.util.List;
import java.util.Map;

@Converter
public class JsonToListMapConverter implements AttributeConverter<String, List<Map<String, Object>>> {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public List<Map<String, Object>> convertToEntityAttribute(String dbData) {
        try {
            return objectMapper.readValue(dbData, new TypeReference<List<Map<String, Object>>>() {});
        } catch (Exception e) {
            ...
        }
    }

}

Update your interface VariationProjection:

@Convert(converter = JsonToListMapConverter.class)
private List<Map<String, Object>> getOption_list;

Now, JPA will automatically use the converter for the option_list field when fetching data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.