show error in one to one mapped field in other tables

Viewed 52

There are three entity class: User, UserProfile, and Country

User is OneToOne mapped to UserProfile UserProfile is OneToMany Mapped to Country

User.java

@Entity
@Data
class Users{
   @Id
   private int id;
    @Column(nullable = false)
    @NotBlank
    private String name;
     
     @OneToOne(cascade = CascadeType.ALL)
     @JoinColumn(name = "alumnus_detail_id")
      private UserProfile userProfile;
     
}

Now Following is UserProfile.java

@Data
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
 public class UserProfile {
  @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private int id;

  @Past(message="You may be a time traveler..")
  @NotBlank
  private Date dob;
   @ManyToOne( cascade = { CascadeType.MERGE,
        CascadeType.DETACH,CascadeType.REFRESH}  )
   @JoinColumn(name="country_id")
  @NotBlank
   private  Country country;
 }

Now following is country.java

@Data
@Getter
@Setter
 @NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="country")
public class Country {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String phonecode;
private String name;

@JsonIgnore
@UpdateTimestamp //hibernate specific feature
private LocalDateTime lastUpdatedDate;

@JsonIgnore
@CreationTimestamp   //hibernate specific feature
private LocalDateTime createdDate;
}

Now when I want to display no validation error for the country using spring:bind. It displays error

Following is profile-form.jsp

                    <form:form action="user-process" method="post"  modelAttribute="user">

<spring:bind path="name">
                        <div class="form-group">
                            <label for="name">Name</label>
                            <form:input class="form-control  ${status.error ? 'is-invalid' : ''}"  id="name" path="name"/>
                            <form:errors path="name" cssClass="invalid-feedback" />

                         </div>
                      </spring:bind>
 <spring:bind path="userProfile.country">
                        <div class="form-group">
                            <label for="country">Country:*</label>
                            <form:select class="form-control ${status.error ? 'is-invalid' : ''}" id="country" path="userProfile.country.id">
                                <form:option value="0">Select  </form:option>
                                 <form:options items="${countryList}" itemValue="id" itemLabel="name"/>
                            </form:select>
                            <form:errors path="userProfile.country" cssClass="invalid-feedback" />
                        </div>
  </spring:bind>
  </form>

The following is userController

@RequestMapping("/user-manage")
public String userUpdate(@ModelAttribute(value = "user")  Users user , ModelMap mapData){

     
    mapData.addAttribute("countryList",countryService.findAll());
     
    return "profile-form";
}

But when Everything else including name validation are working fine rather the country which displays no error for validation

How could display validation error on mapping ?

1 Answers

Add @Valid annotation to the userProfile. This will activate the validation for mapped fields

@OneToOne(cascade = CascadeType.ALL)
 @JoinColumn(name = "alumnus_detail_id")
 @Valid
  private UserProfile userProfile;
Related