Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!

Damon Vessey

Greenhorn
+ Follow
since Jul 11, 2020
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
7
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Damon Vessey

Thank you everyone who helped. This issue has been solved and I will post a link to my github repository if anyone needs to see how I was able to solve this as this seems like a pretty edge case scenario, as I have not found a whole lot on what my program was setting out to accomplish. So hopefully this helps anyone so they don't go through what I did trying to figure it out, even though I feel a great sense of pride and accomplishment. I added functionality for date and time, although I have not worked on validation for checking if a new project has conflicting time with another, but that's for another day.

My GitHub Repository

2 months ago
On my way to work this morning had a thought. Since I am sending a list to the controller for all machines from the database.
In my save route I should be able to request param a list of ids sent from the form even though the list only contains one element and find the Id of the machine from index position [0] sounds kinda counterintuitive.
2 months ago
What I think is happening is a project is being created then saved to the database without the machine being set to the project first. Ill. Try that tonight.
2 months ago
Its possible I am thinking about this the wrong way but in my controller I'm searching for a list of all machines that have been previously created and stored in the database. Passing that list to the form view for project creation. A new project gets created and the machine that was selected should be part of the project, not creating a new machine
2 months ago
The machine is already created and in the database. The machine selected in the form should just set the selected machine to the project.
2 months ago
Alright this should be my last question. When creating a new project from the form, the user selects a machine (that has already been previously created and is stored in the database) once I click submit, a new machine is created in the database and hibernate calls my machine constructor with the name argument to create a new machine and names it the ID of the machine that was selected from the form. A new machine should not be created at this point, as it already exists in the database. I just want the new project to be assigned to the machine that was selected. The only thing that should be happening is the project being saved to the database and storing the machine id that was selected as it's foreign key.

Project Controller


This is the form: In the project class, project has a private Machine machine with getters and setters.


2 months ago
Alright I am getting much closer, now my issue is when saving a project to the database, hibernate is creating a new machine, and naming the machine with the id of the machine that I selected. The project is correctly holding the foreign key to the newly created machine. It's not what was intended but it's a small step in the right direction.
2 months ago
That makes perfect sense! I was trying to combine 2 steps at once, which is why it would never work. I'll give that a shot. Thank you! I'll be back if it works.
2 months ago
I agree it was horrible, but I was desperate and tried every solution I found that could solve my problem even if it was poorly done. So now I have done what you suggested, and hibernate is still giving me fits trying to save null into the project name,
when I haven't tried saving a project yet, just the machine has been saved at this point, trying to get to the form to input project information. The project gets saved after the form is submitted and passed to the /save mapping



Updated project controller



updated machine service



2 months ago
ok so when I go to save a machine in the /addMachine mapping of the project controller



I get this error now:



it's inserting the correct machine id which is 37 in this case, the 179 is the autogenerated projectId. Why does saving the machine, try to save the project also? It's not going to have a name yet because I haven't made it to my form to input those values yet.
2 months ago
My only other thought is hibernate isn't doing what I'm expecting.

I see the insert into project table in the log. it's expecting a machine_id (which is the foreign key) but in my code I am setting an object to an object. Maybe it's not smart enough to know I want the Id from the object?
2 months ago
Sorry for the code dump. I'll target where I think my problem lies.
In my machine entity where I have my list and bi-directional method:



In my project entity



In the project controller



2 months ago
Tim, You are exactly right, hovering over Model, it is coming from spring's UI. I thought it was the object model that gets passed to the form and you act on the object itself. I wasn't aware there was two different things. All my research has pointed to what you guys are saying about linking the two entities. I think i'm expecting too much from spring, and it doesn't know what I want it to do.

I narrowed down to printing the list of projects from the machine entity and it's returning null, even though my code adds a project to that list once machine.setProjects() is called, which adds the project to the arraylist or create one if it's already null. The project is reading the correct machine ID, machine is just not setting the project.
2 months ago
What I'm trying to do: create a project set it to a machine (project has machine's id as a foreign key in the database) and everything is working as intended EXCEPT machine_id column in project database is always null. I've tried every article and video on the internet to get this to work and have had zero luck so far.
Here is my Machine class



Machine Repository



Machine Service



Project class



Project Service



Project Repository



Machine Controller



Project Controller



and the new-projects.html with thymeleaf



In the project controller's /addMachine method (which returns the above new-projects.html), it shows the correct machine id, but once the form is submitted and project gets saved to the database, the machine_id in the database is still null. I don't understand why. Any help would be greatly appreciated. I've been stuck on this for a couple weeks now.
2 months ago