1

I have two projection interface,
First, ProductMinimal.java:

public interface ProductMinimal {
    long getId();
    String getName();
    Long getOwnerId();
    String getOwnerName();
    String getFeaturedImage();
}

and second ProductStatistic.java

public interface ProductStatistic extends ProductMinimal {
    int getTotalVisit();
}

When i use this repository:

Page<ProductStatistic> findMostView(Pageable pageable);

I get result, but wrongly mapped:

[{
    name=1, WRONG
    id=Name, WRONG
    ownerId=administrator, WRONG
    ownerName=21_d20024e8-970f-4738-9cea-8dda22d0afdd.jpg, WRONG
    featuredImage=1, WRONG
    totalVisit=21
}]

it should be:

[{
    name=Name,
    id=1,
    ownerId=1,
    ownerName=administrator,
    featuredImage=21_d20024e8-970f-4738-9cea-8dda22d0afdd.jpg,
    totalVisit=21
}]

but when I put all method in ProductMinimal to ProductStatistic and remove parent interface in ProductStatistic, so ProductStatistic all its own attribute:

public interface ProductStatistic {
    long getId();
    String getName();
    Long getOwnerId();
    String getOwnerName();
    String getFeaturedImage();
    int getTotalVisit();
}

using new this ProductStatistic, now I get expected result.

[{
    name=Name,
    id=1,
    ownerId=1,
    ownerName=administrator,
    featuredImage=21_d20024e8-970f-4738-9cea-8dda22d0afdd.jpg,
    totalVisit=21
}]

I don't like using this way, rewrite all existing property.
What I expect is to be able extends property from ProductMinimal, so i don't need to rewrite code.
Why using interface-based projection that extends other interface projection give wrong result?

Mohd Erwin
  • 33
  • 7
  • looks like you are using an open projection, i would suggest specifying the the `@Value` for each field and make the repository dynamic: `Page findMostView(Pageable pageable, Class type);` where *T* is the projection; for the value for example: `@Value("#{target.totalvisit}") int getTotalVisit();` – Paizo May 29 '19 at 09:47
  • when using @value annotation, i get error: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 – Mohd Erwin May 29 '19 at 10:38
  • As per this link: https://jira.spring.io/browse/DATACMNS-1343], I cann't use @Value annotation with target attribute. Its not available for my query that return individual columns instead return the entire entity. – Mohd Erwin May 29 '19 at 10:56

0 Answers0