Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support javax.validation annotation with Validator for Repository Entity [DATAJDBC-482] #708

Closed
spring-projects-issues opened this issue Feb 3, 2020 · 6 comments
Assignees
Labels
in: repository status: declined type: enhancement

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Feb 3, 2020

MyeongHyeonLee opened DATAJDBC-482 and commented

When an Entity is saved to a Repository, it would be nice to have a javax.validation annotation with the Validator. (like JPA BeanValidationEventListener)

https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/BeanValidationEventListener.java#L103

 

 

@Table
public class Person {
   @Id
   private Long id;
   
   @Size(min = 1, max = 100)
   private String name;

   @PositiveOrZero
   private int age;
}

 

I have implemented Validation with EntityCallback.

However, it would be nice to apply it as a basic  feature in Spring Data JDBC.

Please review the appropriateness of this proposal.

 

 


Affects: 2.0 M2 (Neumann)

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Feb 3, 2020

Jens Schauder commented

Why should validation be tied to persistence?

Shouldn't validation happen at the very beginning of any processing of input while persisting changes happens pretty much at the end?

 

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Feb 4, 2020

MyeongHyeonLee commented

In complex business processes, input data and persistent data can be different.

AggregateRoot is responsible for the invarient of Aggregate.

https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/infrastructure-persistence-layer-design#define-one-repository-per-aggregate

 

I think Aggregate should check the invarient of the final state before it is persisted.

In Spring Data there are features to change entity state inside repository.

(EntityCallback, AuditAware ...)

 

I think the Repository needs validation of the change state on the EntityCallback before giving it persistence.

 

 

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Feb 4, 2020

Jens Schauder commented

I think Aggregate should check the invarient of the final state before it is persisted.

Exactly. But this issue asks to move it into the repository where it doesn't belong in my opinion

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Feb 4, 2020

MyeongHyeonLee commented

Changed the Component in this issue to Repository.

Does it match your request? Or was it another intension?

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Feb 10, 2020

Jens Schauder commented

No my comment wasn't about the component field of Jira it was about where to put validation logic in an application.

There is a very valid use case to put it on the boundary of the system. For example when data enters the system you probably want to validate it. This might be done through bean validation (Spring MVC has support for it https://spring.io/guides/gs/validating-form-input/) or as part of the implementation of an aggregate root as you stated yourself.

But I don't think validating an object when persisting it in the database makes any sense in a well structured application.
If it doesn't match the constraints encoded in the database this is a bug and not a case for validation.
If it is invalid according to your business rules it shouldn't have been created in the first place.

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Feb 10, 2020

MyeongHyeonLee commented

Jens Schauder

Thanks for the feedback.

I will perform validation where I need it.

I will close this issue.

Thank you

@spring-projects-issues spring-projects-issues added status: declined in: repository type: enhancement labels Dec 31, 2020
mp911de pushed a commit that referenced this issue Feb 21, 2022
mp911de added a commit that referenced this issue Feb 21, 2022
Reformat code. Add author tags.

See: #698
Original pull request: #708.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: repository status: declined type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants