This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Modern JavaScript for the Impatient and have Cay Horstmann on-line!
See this thread for details.
Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

POST Data from two or more classes

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make a new call POST on my API on my Spring boot project.
It is very simple when you use only an entity class. But how can you combine, two or more classes?

For example you have a class user: student or teacher .

I would like to have a register for course, in which POST call: create a new course, I need to use
name, lastname, from user class
student_code, gr_year from studentDetails
and last
titleOfCourse , date etc.

If I have
Student.class
Course.class

I need DTO class in order to use on controller and service,
StudentDTO.class
Course.DTO.class

Service:
CourseService.class
StudentService.class

On my services


Now, I don't know what to do.



       CourseDTO newCourseDTO=new CourseDTO(newCourse);
I try  to make another
public void saveCours(CourseDTO course)  {
//with the same way with other classes but I believe that is not the best solution. and as I try it, I didn't run.


Is there any way ?


 
Saloon Keeper
Posts: 12251
259
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your controller action doesn't make sense to me, neither in purpose nor implementation.

You say you want to register for a course. I would write an action such as this:
 
Saloon Keeper
Posts: 22479
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure that "DTO" is appropriate. A Data Transfer Object was an essential part of JEE back when EJBs were specialized class objects, but JPA turned them into POJOs, and at that point the need of a separate transfer object to ferry data in and out of the persistence layer became unnecessary since the JPA entity object could serve  as its own DTO.

It's very common when working with databases to need to work with graphs of data. By "graph" in mathematical/computer science terms we're talking about a network of interconnected objects, not pie and bar charts. A tree structure, for example is what's known as a directed graph and if it doesn't cross-reference itself, it's further qualified as a Directed Acyclic Graph.

Indeed, the schema of a database is just one big graph, so when we work with part of it what we are actually dealing with is a sub-graph. Regardless, if you have an invoice header, line items and maybe inventory references, and you need to work on them as a unit, that's a graph. Or, as I sometimes call it, a working set, which is to say the set of entity objects that I'm going to be working with - adding, deleting, and/or modifiying elements therein, including possibly adjusting their relationships.

When I design a complex app, I have 2 persistence layers. The bottom layer is the Data Access Object (DAO) layer and the classes in it generally perform CRUD and search functions on individual tables. Or sometimes a parent/child set of tables. The layer above that is the Database Logic or Service Layer and it deals with working sets - retrieving, storing, and validating collections of related objects as a unit. Within these 2 layers I am transactional and connected to the database. Above the Service layer is the Business Logic layer, topped by the UI layer. Before passing data to those layers I detach the objects in the working set from the database and end the transaction that was started when the Service Layer method was invoked.

This keeps everything neatly organized. I don't accidentally modify the database in the application layers, since at that point I'm detached. All database operations go through a service method before reaching a DAO, no mater how simple, because a little extra overhead is well paid-for by knowing absolutely that nobody's reaching around corners to do things.

So that is how I can - and frequently do - POST data from multiple database Entity classes.
 
Eleni Pap
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your extensive reference on graph entity, you mean something like



I didn't know about it.

In my example , the project is quite simple, so I haven't problems with DTOs. As, Stephan has mentioned this  part of code:

help me to think how to continue my project.  

When I POST a new course, I should add pathVariable course_id, in order to connect all entities I want.


But you give me a motive for learning.
Thank you !!
 
    Bookmark Topic Watch Topic
  • New Topic