-
Notifications
You must be signed in to change notification settings - Fork 698
Description
Pageable violates open/close principle when using with Entity
refactored to have Embedded
object of properties.
I have
- the controller method taking
Pageable
- repository taking this
Pageable
as argument offindAll()
method MyEntity
— entity class
class MyEntity {
...
var prop1: String? = null
var prop2: String? = null
...
}
I'm sorting from REST API by prop1
value. Pageable(..., sort="prop1:desc")
I refactored MyEntity
using Embeddable
class:
@Embeddable
class MyComplexProp1 {
var prop1: String? = null
}
class MyEntity {
...
@Embedded var complex: MyComplexProp1? = null
var prop2: String? = null
...
}
And now the same call to my HTTP controller fails with
org.springframework.data.mapping.PropertyReferenceException: No property 'prop1' found for type 'MyEntity'
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:94)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:455)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:431)
and I must use Pageable(..., sort="complex.prop1:desc")
from REST API to resolve this problem.
I think that this code behavior is inconsistent, incorrect and violates open/close principle. Why do my controller must know about structure of my entity? Why do the entity with embedded object is not the same (for sorting purposes) as entity without it (they both have same logical attributes, nothing logically have changed)? Embedded classes were added to structure entities to group attributes, not to change set of database properties.
I think it will be better if we will search both paths complex.prop1
and if not found prop1
for classes annotated with @Embedded
. It will be consistent, not break backward compatibility with current Spring Core behavior and gives us the ability not break backward compatibility of REST Controller interface when refactoring database entities.