Reputation: 139
I don't really understand the steps for creating Group Validation using Hibernate. Please can anyone help. I have two classes from databases where some fields from both classes should be grouped and validated, using official hibernate docs I take next steps: Lets assume I have two classes with fields:
public class Tickets implements Serializable {
@NotNull(groups=GroupLotsAndTicketsValidation.class)
@Size(groups = GroupLotsAndTicketsValidation.class)
@NotBlank
private string Amount;
//and other fields..
//getters and setters
}
This was my first class. Here is the Second:
public class Lots implements Serializable {
@NotNull(groups = GroupLotsAndTicketsValidation.class)
@Size(groups = GroupLotsAndTicketsValidation.class)
@NotBlank(groups =GroupLotsAndTicketsValidation.class)
private String title;
@NotNull(groups = GroupLotsAndTicketsValidation.class)
@NotBlank(groups =GroupLotsAndTicketsValidation.class)
private String fullWeight;
//and other fields..
//getters and setters...
}
This was My second class. Also I read that I should create one interface per class. However as I think It should be done if i want to create my own annotations. Should I create interfaces and what steps to take next. Thanks in advance
Upvotes: 3
Views: 5625
Reputation: 7561
Group interface is not for implementation. It's a marker of validation. This marker you can use in tests. If you use powerful frameworks like spring, you can use marker on @Controller
or any bean level.
For example. You have Entity Profile
In your rest API you need to have POST
(create) and PUT
(update) operations. So you'll have
@RestController
public class ProfileController {
@PostMapping("/profile")
public Calendar insert(@RequestBody @Valid Profile profile) {
return null; // implement me
}
@PutMapping("/profile/{profileId}/")
public void update(@PathVariable String profileId, @RequestBody @Valid Profile profile) {
// todo implement me.
}
}
In the entity you have to put id
value before use any update operation (for example only, this case valid for create operation if id-key is managed on java side). So it should not be null
in your validation logic.
public class Profile {
@Id
@NotNull
private Long id;
private String name;
@NotNull
private String email;
}
And if you'll try to use it you'll get ui nonNull constraint exception in create operation. You do not need ID
notNull validation in your create operation, but you need one in your update operation! One of solutions is to create different dto objects with their own validation, other - remove validation from id field, and alternative solution is to use:
public class Profile {
@Id
@NotNull(groups = UpdateOperation.class)
private Long id;
private String name;
@NotNull
private String email;
}
public interface UpdateOperation {}
and add changes into controller (@Valid
(JSR-303) should migrate to validator annotation that supports validation groups spring provides @Validated
):
@PostMapping("/profile")
public Calendar insert(@RequestBody @Validated Profile profile) {
return null; // implement me
}
@PutMapping("/profile/{profileId}/")
public void update(@PathVariable String profileId, @RequestBody @Validated({UpdateOperation.class}) Profile profile) {
// todo implement me.
}
As result you do not need create dto per rest operation and you have flexible validation on your rest level.
Also I hope you already red:
Upvotes: 4