javaspring-boothibernatespring-data-jpaquarkus

Spring Data JPA Interface Projection returns null values


I'm trying to get aggregated data using Spring Data JPA projection I have two entities — UserEntity (with accountNumber and customerID) and RecordEntity, which has a composite key (accountNumber, amountThreshold). I want to sum up amount grouped by customerID and amountThreshold, and map it to a projection interface.

However, even though the SQL query works fine in the database, my projection fields return null (e.g., customerID = null, amountThreshold = 0, amount = null).

Entity example:

@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Table(name = "USER")
public class UserEntity implements Serializable {
    ...
    @Column(name = "ACCOUNTT_NUMBER", nullable = false, length = 20)
    private String accountNumber;
    @Column(name = "CUSTOMER_ID", nullable = true, length = 30)
    private String customerId;
    ...
}

@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Table(name = "RECORD")
public class RecordEntity implements Serializable {
    @EmbeddedId
    private RecordEntityPK id;
    @Column(name = "AMOUNT", nullable = false)
    private BigDecimal amount;
    ...
}

@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@Data
@Embeddable
public class RecordEntityPK implements Serializable {
    @Column(name = "ACCOUNTT_NUMBER", nullable = false, length = 20)
    private String accountNumber;
    @Column(name = "AMOUNT_THRESHOLD", nullable = false)
    private Long amountThreshold;
    ...
}

Projection:

public interface UserAmountProjection {
    String getCustomerId();
    Long getAmountThreshold();
    BigDecimal getAmount();
}

Repository:

public interface RecordEntityRepository extends CrudRepository<RecordEntity, RecordEntityPK> {
    @Query("""
    SELECT
        u.customerId AS customerId,
        r.id.amountThreshold AS amountThreshold,
        SUM(r.amount) AS amount
    FROM RecordEntity r
    JOIN UserEntity u ON r.id.accountNumber = u.accountNumber
    WHERE u.customerId IN ?1
    GROUP BY u.customerId, r.id.amountThreshold
    ORDER BY u.customerId, r.id.amountThreshold
""")
    List<UserAmountProjection> findAmountByCustomerID(List<String> CustomerID);
}

Also, I tried changing the query to return simple values like String and BigDecimal, and it worked — so it looks like the problem is related to projection mapping.

What am I doing wrong?


Solution

  • So, it turned out that the problem was in the multiline string literal for query.

    @Query("""
        SELECT
            u.customerId AS customerId,
            r.id.amountThreshold AS amountThreshold,
            SUM(r.amount) AS amount
        FROM RecordEntity r
        JOIN UserEntity u ON r.id.accountNumber = u.accountNumber
        WHERE u.customerId IN ?1
        GROUP BY u.customerId, r.id.amountThreshold
        ORDER BY u.customerId, r.id.amountThreshold
    """)
    

    Looks like Hibernate adds special characters somewhere inside and then cannot recognize aliases..