spring-data-relational
spring-data-relational copied to clipboard
Spring Data JDBC documentation has no information about SpEL support
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
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.
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
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
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.