Share Attribute Validation Annotation between different Models - spring

I have multiple Models with the same attribute, for example: CustomerNumber it hast to be always 10 digits long, it is now in the Model of Customer, CustomerContract, CustomerContactData, Entity and Restmodels.
I am looking for a possibility to define the Attribute validation only once.
I am using Springboot and Kotlin. ( Value Classes do not work because Spring ignores init and anotations of them on RestController )

Related

Is there another approach or io.swagger annotation that hides fields like how #ApiModelProperty does to hide fields of a custom Object?

I would like to hide for example the id field but NOT by annotating the field itself using #ApiModelProperty but at the ParameterMapper object level. That is, annotating ParameterMapper, since this object is used in multiple places and I am not showing the same fields in all place of the swagger doc.

CakePHP: Why entity validation is on Table class intead of Entity class?

If the CakePHP validations valids entities, why the rules is set on table class intead Entity class?
Because it's the table class that consumes the entities, hence its the tables responsibility to provide the contract that defines what's valid and what's not.

Save multiple row data and some other data into two tables simultaneously in spring hibernate

I want to save data of a form into two tables simultaneously. The form contains multiple identical rows with some fields like
item, unit, unit_price, total.
1.) In the table1 I would like to save some data like id(auto generated, primary key), date, creator.
2.) In table2 I would like to save the id(auto generated, primary key), fid(id of the table1, foreign key), item, unit, unit price, total. Remember, in table2 multiple rows will be save from the form data.
I can't figure it out. Please help me with a valid step by step example, and please don't use static or pre-defined data, take the data from form only.
Is your form an HTML form on a web page, and are you writing a Spring MVC application? Have you setup Spring Security, or some other authentication mechanism such that you know who the user is? If so, here is one approach:
Define a plain old Java object (POJO) with fields id, unit, unit-price, total, creator, currentDate; perhaps you would name it Item.
Define another plain old Java object perhaps named "ItemHistory", with its own ID and a reference to an Item.
In the first POJO ("Item") include a reference to a list of ItemHistorys.
Annotate both classes with Hibernate/JPA annotations, such that the Item has a OneToMany relationship to ItemHistory.
Define a Spring MVC controller with a method annotated as a POST method, with an Item as one parameter, and an Authentication as another parameter (supposing you have configured Spring Security, the Authentication will automatically be populated with the identity of the currently-logged-in user. In this method, set the currentDate and author of the Item to the current date and the principal from the Authentication parameter. If it is a new Item (no id yet), add the first ItemHistory as a copy of the Item itself. If it is an existing item, add another ItemHistory if needed.
Define a Spring bean, perhaps named ItemService, with a method annotated as transactional, that takes an Item and saves it. Call this bean from the REST controller. Such a Spring bean is known as a DAO (data access object).
There are other strategies, but if you already have Spring MVC, Spring Security, and Hibernate/JPA configured to your needs, this one is pretty straight-forward.

Can hibernate validator be used to validate a field depending on a field value from another class

I have a scenario where I need to validate some fields of a class depending on the value of a field from another class.
For ex: Street, City, Postal Code of Address class should not be null if the Employee has registered for a Group Retirement Plan.
Can this scenario be handled using Custom Validator of Hibernate Validator? Is this a good candidate for Hibernate Validator ? If so, could anyone provide me with some examples.
I have already gone through cross field validation examples like:
Cross field validation with Hibernate Validator (JSR 303)
If I have a boolean indicator to indicate whether a user is registered for group retirement plan in Address domain class, I know I can come up with Custom Validator to verify the fields. But, I want to avoid adding indicator to Address class. As well, Address is not a field in Employee class to do nested validation.
The Frameworks I am using are Spring MVC and Mybatis for Data access. The validation is happening at Spring Rest API call with #Valid annotation.
I guess a custom class level constraint might work, but it depends on your actual domain classes. It is currently not clear how for example Employee, Group Retirement Plan and these other entities are connected.
Whether Hibernate Validator is a good fit is whether you have other validations as well or which other frameworks are involved. For example, if you want validation to take place at JPA live cycle events, then Bean Validation is a good fit, since it integrates per specification with JPA. If you really only have this one validation, writing some custom code might be all you need.

Can i write two separate pojo classes one for validation and another for hibernate mapping

If yes, how to map them. I am using jsr bean validation and hibernate mapping annotations.
You don't have to do this by mapping two classes to one table.
You can just define one class A which map to a table as a PO (Persistent Object), and define another class B which transfer values amgonst View, Controler and Service Layer as a DTO/VO (Data Transfer Object/Value Object) for validation or data transfering. use BeanUtils to copy properties between PO and DTO.

Resources