I used SpringBoot 1.5.x

    @Query("select id,stock from CommoditySku  where entityId = ?1")
    List<CommoditySkuStockDto> getByEntityId(long commodityId);

    public interface CommoditySkuStockDto {
        long getId();

        int getStock();
    }

SpringBoot Dynamic Projections error Null return value from advice does not match

it worked.

but now , I use Spring Boot 2.0.9

    @Query("select id,stock from CommoditySku  where entityId = ?1")
    List<CommoditySkuStockDto> getByEntityId(long commodityId);

    public interface CommoditySkuStockDto {
        long getId();

        int getStock();
    }

SpringBoot Dynamic Projections error Null return value from advice does not match

org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for:

Comment From: wilkinsona

Thanks for the report. Spring Boot 2.0.x is no longer supported. Please upgrade to the latest Spring Boot 2.1.x or 2.2.x release and, if the problem still occurs, provide a small sample application that reproduces it.

Comment From: shenjianeng

sorry, my project is hard to use Spring Boot 2.1.x.

please give me some time to update.

I will create a demo project to test.

thanks~

Comment From: qianhangkang

Thanks for the report. Spring Boot 2.0.x is no longer supported. Please upgrade to the latest Spring Boot 2.1.x or 2.2.x release and, if the problem still occurs, provide a small sample application that reproduces it.

This is a demo for SpringBoot 2.2.3.RELEASE

@Getter
@Setter
@Entity
@Table(name = "user")
public class User implements Serializable {

    @Id
    private long id;

    private String name = Strings.EMPTY;

    public interface UserInterface{

        long getId();

    }

    public static User of(long id, String name) {
        User user = new User();
        user.setId(id);
        user.setName(name);
        return user;
    }
}
public interface UserRepository extends CrudRepository<User,Long> {
    @Query(value = "select id from User where id =?1")
    User.UserInterface getIdById(long id);
}
    @org.junit.jupiter.api.Test
    void name() {
        userRepository.save(User.of(1, "hello world"));
        System.out.println(userRepository.findById(1L).orElse(null).getId());
        System.out.println(userRepository.getIdById(1L).getId());
    }

and then run it SpringBoot Dynamic Projections error Null return value from advice does not match

Comment From: shenjianeng

Thanks for the report. Spring Boot 2.0.x is no longer supported. Please upgrade to the latest Spring Boot 2.1.x or 2.2.x release and, if the problem still occurs, provide a small sample application that reproduces it.

This is a demo for SpringBoot 2.2.3.RELEASE

```java @Getter @Setter @Entity @Table(name = "user") public class User implements Serializable {

@Id
private long id;

private String name = Strings.EMPTY;

public interface UserInterface{

    long getId();

}

public static User of(long id, String name) {
    User user = new User();
    user.setId(id);
    user.setName(name);
    return user;
}

} ```

java public interface UserRepository extends CrudRepository<User,Long> { @Query(value = "select id from User where id =?1") User.UserInterface getIdById(long id); }

java @org.junit.jupiter.api.Test void name() { userRepository.save(User.of(1, "hello world")); System.out.println(userRepository.findById(1L).orElse(null).getId()); System.out.println(userRepository.getIdById(1L).getId()); }

and then run it SpringBoot Dynamic Projections error Null return value from advice does not match

thank you for your help ~

Comment From: wilkinsona

Thanks for the sample, @shenjianeng. I have reproduced the problem but I can't see if it would not have occurred with Spring Boot 1.5 as you have not shown what you were doing there.

Your query doesn't look quite right to me. You are selecting only the ID and then requiring Spring Data to try and create a User.UserInterface from that. I believe it should be something like select u from User u where u.id =?1 instead.

I'm going to close this issue now as I can't see any sign of a problem caused by Spring Boot. If you'd like some assistance with Spring Data JPA's querying, please ask on Stack Overflow or the #spring-data channel on Gitter. If you believe you have found a bug or regression in Spring Data JPA, please open a Spring Data JPA JIRA ticket.

Comment From: stefmil

@wilkinsona not sure if this should be closed. The query is quite simple and correct, it's obvious that @shenjianeng has built a simple Demo app to demonstrate the issue he/she is getting. The reason why only id is returned is because of using Spring Data JPA Projection.

I am also experiencing the same issue when trying to use projection:

Null return value from advice does not match primitive return type for: public abstract int com.example.repositories.ExampleRepository$ExamplePlus.getLikesNo()

SpringBoot version 2.2.2.RELEASE

Could you please revisit this issue?

Comment From: wilkinsona

@stefmil As I said above, if you believe you have found a bug or regression in Spring Data JPA, please open a Spring Data JPA JIRA ticket.

Comment From: bmvsprasad

The issue is very old . But still i want to share my few cents on it. If it could help anyone.

The issue occurs when a primitive type java is mapped to a primitive type at database.

JPA will fetch the data from backend as objects. If a primitive type like long/int/float has null at backend. The null value is assigned to java primitive type long/int/float

private long id; long id= null (from backend) / this will throw exception. as null is not fit into primitive type.

If there is no null value for id variable from backend , it will work properly. If there is null from backend it will throw exception.

Test this if you change the private long id to wrapper class. This exception will not occur.

instead of private long id; use below

private Long id; // then you will not see this exception null advice for primitive type.