bean-validation

JSR-303 dependency injection and Hibernate

让人想犯罪 __ 提交于 2019-11-26 20:16:27
问题 Spring 3.0.2, Hibernate 3.5.0, Hibernate-Validator 4.0.2.GA I am trying to inject Spring dependencies into a ConstraintValidator using: @PersistenceContext private EntityManager entityManager; I have configured the application context with: <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/> Which, according to the Spring documentation, should allow “custom ConstraintValidators to benefit from dependency injection like any other Spring bean”

javax.servlet.ServletException: HV000030: No validator could be found for type: java.lang.Integer

こ雲淡風輕ζ 提交于 2019-11-26 20:07:55
问题 I have to update information in my database. FacadePatient.java class code: public Patient update(Patient p) { Patient pat = em.find(Patient.class, p.getPatientId()); p.setPatientPhone(pat.getPatientPhone()); p.setPatientDateNaiss(pat.getPatientDateNaiss()); p.setPatientEmail(pat.getPatientEmail()); p.setPatientJob(pat.getPatientJob()); p.setPatientSmoking(pat.getPatientSmoking()); p.setPatientSize(pat.getPatientSize()); em.merge(pat); return p; } 回答1: HV000030: No validator could be found

Java Bean Validation (JSR303) constraints involving relationship between several bean properties

Deadly 提交于 2019-11-26 19:44:50
问题 Say I have the following simple java bean: class MyBean { private Date startDate; private Date endDate; //setter, getters etc... } Is there a mechanism in JSR 303 to create a custom validator that validates the constraint that startDate must be before endDate? It seems to me to be a common use case, yet I cannot find any examples of this kind of multiple property relationsship constraint. 回答1: I can think of a few things to try. You could create a Constraint with a target of the type itself

In Hibernate Validator 4.1+, what is the difference between @NotNull, @NotEmpty, and @NotBlank?

霸气de小男生 提交于 2019-11-26 18:43:36
问题 I can't seem to be able to find a summary that distinguishes the difference between these three annotations. 回答1: @NotNull : The CharSequence, Collection, Map or Array object is not null , but can be empty. @NotEmpty : The CharSequence, Collection, Map or Array object is not null and size > 0 . @NotBlank : The string is not null and the trimmed length is greater than zero . To help you understand, let's look into how these constraints are defined and carried out (I'm using version 4.1): The

JSR 303 Validation, If one field equals “something”, then these other fields should not be null

烈酒焚心 提交于 2019-11-26 17:02:52
I'm looking to do a little custom validation with JSR-303 javax.validation . I have a field. And If a certain value is entered into this field I want to require that a few other fields are not null . I'm trying to figure this out. Not sure exactly what I would call this to help find an explanation. Any help would be appreciated. I am pretty new to this. At the moment I'm thinking a Custom Constraint. But I'm not sure how to test the value of the dependent field from within the annotation. Basically I'm not sure how to access the panel object from the annotation. public class StatusValidator

How to I get Spring-Data-MongoDB to validate my objects?

大兔子大兔子 提交于 2019-11-26 16:34:40
问题 I have a very simple Spring Boot application that uses Spring-Data-Mongodb All I want to do is set a JSR-303 validation rule that says the object I'm saving must have a username. I read that JSR-303 was added to spring-data-mongodb in version 1.1 so I assumed that when I save an object it's validated but this isn't the case. Does anyone have a simple example setup that shows how this works? My User pojo looks like public class User { @Id private String id; @NotNull(message = "User Name is

Hibernate Validation of Collections of Primitives

家住魔仙堡 提交于 2019-11-26 15:08:28
I want to be able to do something like: @Email public List<String> getEmailAddresses() { return this.emailAddresses; } In other words, I want each item in the list to be validated as an email address. Of course, it is not acceptable to annotate a collection like this. Is there a way to do this? Neither JSR-303 nor Hibernate Validator has any ready-made constraint that can validate each elements of Collection. One possible solution to address this issue is to create a custom @ValidCollection constraint and corresponding validator implementation ValidCollectionValidator . To validate each

Validate class level bean validation constraints in JSF

寵の児 提交于 2019-11-26 14:56:00
问题 It seems that JSF 2.0 does not call "class level constraints". Quoting from an SO answer JSF 2.0 doesn't call class level validation constraints. From JSF validation: JSF 2 provides built-in integration with JSR-303 constraints. When you are using bean validation in your application, JSF automatically uses the constraints for beans that are referenced by UIInput values. The answer furthermore suggests using SeamFaces to validate the class-level constraints anyways. Unfortunately this is a non

How to disable Hibernate validation in a Spring Boot project

淺唱寂寞╮ 提交于 2019-11-26 14:15:33
问题 I have a spring boot project that has a CrudRepository, an Entity and a Controller. I am basically trying to persist an entity based on the data passed to the Controller. To do this, I am using spring-boot-starter-jpa . My Entity is annotated with JSR-303 annotations, which are checked in the controller before the data gets passed to the CrudRepository for persistence. Controller method: @RequestMapping(value = "users", method = { RequestMethod.POST }) public SuccessfulResponse<User> addUser(

Spring MVC - @Valid on list of beans in REST service

╄→尐↘猪︶ㄣ 提交于 2019-11-26 12:08:51
问题 In a Spring MVC REST service (json), I have a controller method like this one : @RequestMapping(method = RequestMethod.POST, value = { \"/doesntmatter\" }) @ResponseBody public List<...> myMethod(@Valid @RequestBody List<MyBean> request, BindingResult bindingResult) { Where the MyBean class has bean validation annotations. The validations don\'t seem to take place in this case, although it works well for other controllers. I don\'t want to encapsulate the list in a dto this that would change