I have built my controller, client and address class(models), and templates to index, and add. when I try to test for validation, spring returns:
There was an unexpected error (type=Internal Server Error, status=500). Validation failed for classes [com.pooltracker.models.Address] during persist time for groups [javax.validation.groups.Default, ] List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='Invalid', propertyPath=zipCode, rootBeanClass=class com.pooltracker.models.Address, messageTemplate='Invalid'} ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=state, rootBeanClass=class com.pooltracker.models.Address, messageTemplate='{javax.validation.constraints.NotNull.message}'} ConstraintViolationImpl{interpolatedMessage='Can not be empty', propertyPath=street, rootBeanClass=class com.pooltracker.models.Address, messageTemplate='Can not be empty'} ConstraintViolationImpl{interpolatedMessage='Can not be empty', propertyPath=city, rootBeanClass=class com.pooltracker.models.Address, messageTemplate='Can not be empty'} ]
I have come to learn that hibernate can not handle validating the address class. I have stumbled upon the topic of bean validation. I am new to this and trying to navigate the best way to solve my issues without going too far down the rabbit hole at the moment. below is my controller, and models.
@Controller
@RequestMapping("clients")
public class ClientController {
@Autowired
private ClientDao clientDao;
@Autowired
private AddressDao addressDao;
@RequestMapping(value = "")
public String index(Model model) {
model.addAttribute("clients", clientDao.findAll());
model.addAttribute("title", "My Clients");
return "clients/index";
}
@RequestMapping(value = "add", method = RequestMethod.GET)
public String displayAddClientForm(Model model) {
model.addAttribute("title", "Add Client");
model.addAttribute(new Client());
return "clients/add";
}
@RequestMapping(value = "add", method = RequestMethod.POST)
public String processAddClientForm(@ModelAttribute @Valid Client newClient,
Errors errors, Model model) {
if (errors.hasErrors()) {
model.addAttribute("title", "Add Client");
model.addAttribute("client", newClient);
return "clients/add";
}
clientDao.save(newClient);
return "redirect:";
}
}
Client
entity:
@Entity
@Table(name = "client")
public class Client {
@Id
@GeneratedValue
@Column(name = "client_id")
private int id;
@NotNull
@Size(min=1, message = "Client must have a first name")
@Column(name = "first_name")
private String firstName;
@NotNull
@Size(min=1, message = "Client must have a last name")
@Column(name = "last_name")
private String lastName;
@OneToOne(cascade = CascadeType.ALL,
fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "address_id")
private Address address;
@NotNull
@Size(min=10, max=10, message = "Must be 10 digits only")
private String phone;
// setters/getters
}
Address
entity:
@Entity
@Table(name = "address")
public class Address {
@Id
@GeneratedValue
@Column(name = "address_id")
private int id;
@NotNull
@Size(min=1, message = "Can not be empty")
@Column(name = "street")
private String street;
@NotNull
@Size(min=1, message = "Can not be empty")
@Column(name = "city")
private String city;
@NotNull
@Enumerated(EnumType.STRING)
@Column(name = "state")
private State state;
@NotNull
@Size(min=5, max=5, message = "Invalid")
@Column(name = "zip_code")
private String zipCode;
@OneToOne(mappedBy = "address" , fetch = FetchType.EAGER)
//@JoinColumn(name = "client_id")
public Client client;
// setters and getters
}
In Client
class, add @Valid
to address
field:
@Valid
@OneToOne(cascade = CascadeType.ALL,
fetch = FetchType.EAGER,
optional = false)
@JoinColumn(name = "address_id")
private Address address;
@Valid
javadoc says:
public @interface Valid
Marks a property, method parameter or method return type for validation cascading.