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

Spring Data JDBC documentation has no information about SpEL support #1719

Closed sergey-morenets closed 1 month ago

sergey-morenets commented 7 months ago

Hi

Spring Data JDBC 3.2.0 introduced SpEL support for @Table/@Column annotations: https://github.com/spring-projects/spring-data-relational/issues/1325

However current reference documentation contains no information about how to use this feature and what are the benefits: https://docs.spring.io/spring-data/relational/reference/jdbc/mapping.html

christophstrobl commented 7 months ago

Thanks for bringing this up. There's a litte documentation in the release notes which should also be added to the Naming Strategy section of the reference documentation.

sergey-morenets commented 7 months ago

Thank you, @christophstrobl

I asked about documentation because this feature just doesn't work for me.

I added a Spring bean:

    @Bean
    String productName() {
        return "PRODUCTS";
    }

And then used this bean in the table name:


@Table("#{productName}")
public class Product implements Persistable<Integer> {

However I got an exception:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'productName' cannot be found on null

sergey-morenets commented 7 months ago

I also tried to declare another Spring bean:

    @Bean
    Product product() {
        Product product = new Product();
        product.setName("PRODUCTS");
        return product;
    }
@Table("#{product.name}")
public class Product implements Persistable<Integer> {

But still got an exception:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'product' cannot be found on null

mp911de commented 7 months ago

I filed a ticket to update our docs. By default, the mapping layer does not define a root object as we need to construct column and table names before even having an instance of an entity. Please have a look at org.springframework.data.spel.spi.EvaluationContextExtension (see https://github.com/spring-projects/spring-security/blob/main/data/src/main/java/org/springframework/security/data/repository/query/SecurityEvaluationContextExtension.java for a reference implementation) on how to contribute functions and objects into Spring Data's SpEL realm.