So, I am starting with springboot and have medium knowledge about java. My problem is that I have an entity (or Model, I don't know how it is called) that is called Personnel, this entity have a foreign key to another entity called Role, like this:
@Entity(name = "Personnel")
public class PersonnelEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "first_name", length = 50, nullable = false)
private String firstName;
@Column(name = "last_name", length = 50, nullable = false)
private String lastName;
//.... other properties
@ManyToOne()
@JoinColumn
(
name = "role_id",
//nullable = false,
foreignKey = @ForeignKey
(
name = "FK_Personnels_role_id",
foreignKeyDefinition = "FOREIGN KEY (role_id) REFERENCES Roles (id) ON DELETE RESTRICT ON UPDATE CASCADE"
)
)
private RoleEntity role;
And, I have a DTO for the Personnel, that has all the validations except the RoleName validation:
public class PersonnelDto {
@NotBlank
private String firstName;
@NotBlank
private String lastName;
//... other properties
@NotBlank
private String roleName;
The json from a request is parsed to the DTO under the hood apparently, but I want to control that, when making a request, the client would send the name of the role in the json, and with an annotation or something else, that name would be checked to see if it is present in the database (as it is unique), if not the dto would not pass through the @valid annotation in any request.
Also, I was considering that maybe in the DTO, I would not have a String roleName, but the actual RoleEntity, like this:
public class PersonnelDto {
@NotBlank
private String firstName;
@NotBlank
private String lastName;
//... other properties
@NotBlank
private RoleEntity role;
But, I have no idea how to code so that the roleName in json gets automatically queried and converted to the whole RoleEntity, I just know that there has to be a way to do so, as for example you can put Date in a DTO and the json string of a date is converted automatically to a Date object.
So, in my case the json:
{
"firstName": "val1",
"lastName": "val2",
...,
"roleName": "roleName"
}
would be converted to that DTO up there.
Resuming, how can I make a role name in a json be checked present in databaseand if so convert it to a RoleEntity object in the DTO? Also, if it is not present then the DTO would be considered invalid.
You can't validate if there is another role directly in the parsing JSON to DTO process. You have to do that in your service after passing through the controller. Something like this:
In your controller use the @Valid
annotation in your endpoint
@PostMapping()
public ResponseEntity<Object> createPersonnel(@RequestBody @Valid PersonnelDto personnelRequest){
// code here...
personnelService.create(personnelRequest);
// code here...
}
Now, in your service you can read the roleName from the DTO and validate it:
public void create(PersonnelDto personnelRequest){
// code here...
Optional<RoleEntity> role = roleRepository.findByName(personnelRequest.getRoleName());
if (role.isPresent()) {
// If there is another role with the same name throw an error here...
{
// Otherwise continue with your normal flow here...
}
As a result, you will have a readable code that can be tested using unit tests libraries like JUnit
.