Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Spring with Hibernate Getting Started with CRUD in Spring + Hibernate Controller Method for Adding a Category

Sergei Makarov
Sergei Makarov
13,839 Points

Category doesn't have any Constructor , but the default Constructor (without any parameters). How it will be created?

Hello,

Quote from the video: "And because we'll assume, that the form data will include all necessary fields for a Category object to be constructed...".

But there is only one constructor with no any field. How it should be constructed?

I believe, it just compile a new Object Category with a help of default constructor and set all the parameters from the field with setters? How it works actually in background? Why not using second constructor for that very purpose?

Thank you!

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

Yes, you are correct. The Category object will be created with the default constructor and the relevant fields will be filled in with setters. It is a fairly common pattern in Java to map properties to getters and setters so that some propertyName in a database, form data, JSON response, etc. will have a matching getPropertyName() and/or a setPropertyName() method in the corresponding Java object. These properties can be marked by annotations (like JPA) or be searched for by reflection (ie, lookup all the class's methods starting with 'get' and 'set').

So, now what about using a second constructor? The trouble with a non-default constructor is how exactly to know it exists and how to use it. The constructor is used by the Spring framework itself, which passes the Category into our controller. So the process it out of our hands in this case. It's possible in our own code to pick up on other constructors through reflection but 1) if you have multiple parameters of the same type, how do you know what order they need to be in? 2) How do you choose between the different constructors? Ultimately it's better to lean on the existing standards in this instance.

Sergei Makarov
Sergei Makarov
13,839 Points

Thank you Seth,

Now it is much more clear for me.

Could you please explain me the next pattern:

In next video we are setting Form for 'addCategory'-request. For this purpose we use thymeleaf as th:object (references to ' new Category()' and then th:value (references to the class' fields as 'name' and 'colorCode') , as I believe, to set this fields to a very new Category via setters by submitting our form. So my question ist: how thymeleaf engine knows, that it should be used setters and not the getters? -> Is it a case, cause we are using POST-method, declared by form? And for each GET-method it will be used only getters per se? So does work the logic of Thymeleaf-engine?

Thank you!