JPA validation API - validation

Does JPA contains any validation API?
E.g. email property validation like this in hibernate validation:
#Email
public String getUserEmail() {
...
}

Bean Validation (JSR303) can be used nicely alongside JPA, but is a separate API. This is a very brief introduction to Bean Validation itself, and see this for how an introduction of how it can be used with JPA.
Note: Similar to JPA, you will have to pick an implementation, of which there are many. Hibernate Validator is the reference implementation.

Related

Spring TypeFilter for method annotations to be used in ClassPathScanningCandidateComponentProvider#findCandidateComponents

Is there an existing Spring org.springframework.core.type.filter.TypeFilter implementation for Method annotations?
I am looking to call ClassPathScanningCandidateComponentProvider#findCandidateComponents in order got get all of the components that have an annotation or inherited annotations so I can validate the state of the configuration supports the annotation attribute values.
There is an org.springframework.core.type.filter.AnnotationTypeFilter, is there a similar implementation but getting classes that have methods that are annotated with the annotation?
If not is there a mechanism to search for components with methods with a specified annotation?

#Transient - Why data is getting saved in Database?

I have an entity variable annotated with #Transient like below. Which means it should not be stored in the Database.
#Transient
private String passwordConfirm;
But when I go to H2-Console, I can see the data is saved there.
Why so? and How can I avoid it?
You're probably using #org.springframework.data.annotation.Transient.
Change it to the right import: #javax.persistence.Transient
This will do the job.
#javax.persistence.Transient is used by the persistence provider (eg.: Hibernate). The persistence provider looks for JPA spec annotations. #org.springframework.data.annotation.Transient is not a part of the JPA spec, so, the persistence provider ignores it.
The #org.springframework.data.annotation.Transient is intended to be used when Spring Data is the actual ORM. Some examples are Spring Data Elasticsearch and Spring Data MongoDB. These Spring Data implementations use the #org.springframework.data.annotation.Transient just like Hibernate uses the #javax.persistence.Transient - not mapping the marked field into the database.

How does a spring converter get a transactional scope?

I was wondering, how a spring converter implementation gets an transactional scope.
Having a Converter which is used to convert a path placeholder (entity id) in an (rest) controller to the Entity itself. The Entity thus is loaded via Hibernate from the database.
For the details:
spring boot 2.3.2
A rest controller endpoint which triggers the converter to expand a path-placeholder (prior executing the rest-endpoint method body)
If in question, the controller method is not annotated #Transactional either
Question:
How (since we disabled OSIV) does this Converter get it's Transactional scope if the convert neither the method/nor the converter class is annotated using Transactional nor the convert method does custom transaction handling?
If you mean how Spring Data repositories get a transaction, the answer is all repository methods are transactional by default.

#Valid working without spring-mvc?

I'd like to use #Valid annotation to validate my beans in controller method. Unfortunatelly it does not work. I know that in order to make it work I'd have to include spring-mvc into my project and put there mvc:annotation-driven or #EnableMvc... .
But I do not use spring-mvc! I use Wicket framework. How to make #Valid working without incorporating spring-mvc?
Thanks!
#Valid is not specific to spring it is an implementation of JSR 303 bean validation. You can use any other reference implementation or write your own. e.g Apache and Hibernate Validator has reference implementation available. Take a look at this answer Is there an implementation of JSR-303 (bean validation) available?

FlushMode in JPA with Spring

I have some problems with FlushMode.AUTO in JPA/Hibernate, because i need change a value of a JavaBean and process a special validation. If validation is ok the bean is updated in DB, if validation fail the bean cannot be updated, like this:
MyBean bean = getBeanFromDB();
bean.setNewNumber(12);
//Before call isValid the bean already updated in DB
if (isValid(bean)){
update(bean);
}
But before validation begins, the Hibernate process the AUTO-UPDATE in my Bean and i don't wanna it. So my solution is setting FlushMode.COMMIT in JPA, but i'm using Spring and i don't know how can i do it in CONFIGURATION (XML) mode.
I see two options:
use bean validation (aka JSR-303) and its JPA integration to ensure automatic validity
use #PrePersist and #PreUpdate events to have your validation logic invoked automatically

Resources