Pathfinder
Pathfinder

Reputation: 994

Spring MVC Bind Nested Classes

I have a requirement wherein i need to save the values of 3 entities from UI to database. As hierarchy shown below

Company->Employees
Company->Users

Company being the parent entity which has two child tables Employees and Users

Now I need to design a single screen to accept all the three table fields from it, I can have all the fields in one PO and then before saving i can directly map to each entity while saving to DB.

But I wanted to have a separate classes for each entity Like

public class Company{
   private String companyName;
   private String companyAddress;
   .....
   private Employee employee;
   private User user;
}

Now my question is how do I map the employee and user attributes in UI and get the values directly on submit from jsp/html.

Upvotes: 1

Views: 174

Answers (1)

Pathfinder
Pathfinder

Reputation: 994

I figured it out myself.

Using thymeleaf i was able to bind the object as shown below

th:field="*{company.employee.name}" 

and also initializing the child object whenever the parent object is initialized.

Upvotes: 1

Related Questions