spring-projects / spring-data-relational

Spring Data Relational. Home of Spring Data JDBC and Spring Data R2DBC.
https://spring.io/projects/spring-data-jdbc
Apache License 2.0
753 stars 345 forks source link

field value is null when alias are equals to property name #1701

Open kfyty opened 8 months ago

kfyty commented 8 months ago

When the field alias in SQL is equal to the field name in the class, the property value in the class will be null. This submission has fixed this issue. eg: the class:

@Data
public class User {
    @Id
    private Long id;
    private Integer fansNum;
}

the interface:

@Repository
public interface UserRepository extends R2dbcRepository<User, Long> {
    @Query("select id, fans_num as fansNum from user where id = :id")
    Mono<User> getById(Long id);
}

the query result of getById(id) is: id=1,fansNum = null.

schauder commented 8 months ago

We expect the selected column to have the proper column name, not the property name, which I think is correct since we are on the database side of things.

Making this more lenient might seem nice at first but might trigger more problems. For example when there are properties propertyname and propertyName and one wants to select only propertyname, then propertyName will get assigned the same value.

While this is a somewhat contrived example I think it demonstrates the conceptual problem of not keeping column names apart.

One could envision some kind of special syntax to make mapping more easy like for example $col(<propertyPath>)$, which could also properly handle embedded entities and similar which currently are kind of difficult to get right.

kfyty commented 8 months ago

We expect the selected column to have the proper column name, not the property name, which I think is correct since we are on the database side of things.

Making this more lenient might seem nice at first but might trigger more problems. For example when there are properties propertyname and propertyName and one wants to select only propertyname, then propertyName will get assigned the same value.

While this is a somewhat contrived example I think it demonstrates the conceptual problem of not keeping column names apart.

One could envision some kind of special syntax to make mapping more easy like for example $col(<propertyPath>)$, which could also properly handle embedded entities and similar which currently are kind of difficult to get right.

Okay, but in my unit testing, when there are properties propertyname and propertyName and one wants to select only propertyname, the result of propertyName is null, it will not assigned the same value.