A
A
andrey_chirkin2022-03-23 08:55:36
Java
andrey_chirkin, 2022-03-23 08:55:36

How to form a data structure and return it to the client?

Hello. I need such a data structure on the client, which I need to get from the PostgreSQL database:

{
      id: nanoid(),
      numberOperation: '010',
      nameOperation: 'Токарная',
      workshop: 6465,
      area: 7,
      OO: true,
      OTK: false,
      PZ: true,
      KPS: true,
      transition: [
        {
          id: nanoid(),
          nameTransition: 'Закрепить деталь',
          executor: [
            {
              id: nanoid(),
              nameExecutor: '4784',
              tsht: '4',
              tpz: '9',
              test: '7',
              tshtCalculated: '',
              tpzCalculated: '',
              testCalculated: '',
              kvr: '973'
            },
          ]
        },

      ],
}

The database structure looks like this (I marked the tables that are needed in red):
623ab5ad13732890217378.png
How to form such a structure on the Java Spring framework using ORM and return it to the client? One-to-many relationship?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2022-03-24
Hasanly @azerphoenix

Good afternoon.
Please tell me, do I understand correctly that you have a table with data, as well as the entities themselves (entity) and you want to return the result received from the database to the client?
Those. I'm interested in the following: db first or code first?
If you have code first, then you have entities and you need to create additional. classes (DTOs). Further, the received data from the database should be mapped into DTO.
For mapping (after the DTOs are created), you can use the add. either: ModelMapper, MapStruct, or interface Converter<S,T>
If you want to create a dto based on the json file you have, you can use an online service or any of them.
Service - https://www.jsonschema2pojo.org/
By the way, are you sure that you have json data in your question?
Here is the json:

{
    "id": 1,
    "numberOperation": "010",
    "nameOperation": "Токарная",
    "workshop": 6465,
    "area": 7,
    "OO": true,
    "OTK": false,
    "PZ": true,
    "KPS": true,
    "transition": [
        {
            "id": 1,
            "nameTransition": "Закрепить деталь",
            "executor": [
            {
                "id": 1,
                "nameExecutor": "4784",
                "tsht": "4",
                "tpz": "9",
                "test": "7",
                "tshtCalculated": "",
                "tpzCalculated": "",
                "testCalculated": "",
                "kvr": "973"
            }, ]
        },

    ]
}

And here is the dto built based on the above json (note that the code most likely needs to be tweaked to suit your needs)
DTOs
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.List;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"numberOperation",
"nameOperation",
"workshop",
"area",
"OO",
"OTK",
"PZ",
"KPS",
"transition"
})
@Generated("jsonschema2pojo")
public class Example {

@JsonProperty("id")
public Long id;
@JsonProperty("numberOperation")
public String numberOperation;
@JsonProperty("nameOperation")
public String nameOperation;
@JsonProperty("workshop")
public Long workshop;
@JsonProperty("area")
public Long area;
@JsonProperty("OO")
public Boolean oo;
@JsonProperty("OTK")
public Boolean otk;
@JsonProperty("PZ")
public Boolean pz;
@JsonProperty("KPS")
public Boolean kps;
@JsonProperty("transition")
public List<Transition> transition = null;

}
-----------------------------------com.example.Executor.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"nameExecutor",
"tsht",
"tpz",
"test",
"tshtCalculated",
"tpzCalculated",
"testCalculated",
"kvr"
})
@Generated("jsonschema2pojo")
public class Executor {

@JsonProperty("id")
public Long id;
@JsonProperty("nameExecutor")
public String nameExecutor;
@JsonProperty("tsht")
public Long tsht;
@JsonProperty("tpz")
public Long tpz;
@JsonProperty("test")
public Long test;
@JsonProperty("tshtCalculated")
public String tshtCalculated;
@JsonProperty("tpzCalculated")
public String tpzCalculated;
@JsonProperty("testCalculated")
public String testCalculated;
@JsonProperty("kvr")
public Long kvr;

}
-----------------------------------com.example.Transition.java-----------------------------------

package com.example;

import java.util.List;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"nameTransition",
"executor"
})
@Generated("jsonschema2pojo")
public class Transition {

@JsonProperty("id")
public Long id;
@JsonProperty("nameTransition")
public String nameTransition;
@JsonProperty("executor")
public List<Executor> executor = null;

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question