保存到 mysql 数据库时,值从双精度更改为整数

发布于 2025-01-11 14:59:36 字数 16532 浏览 0 评论 0原文

实体: 模拟:

package Simulations.Entity;

import lombok.*;

import javax.persistence.*;
import java.util.List;

@Builder
@Table
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Simulation {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column
    private Long id;

    @Column(name = "N")
    private String simulation_Name;

    @Column(name = "P")
    private Double population_Size;

    @Column(name = "I")
    private Double initial_Infected_Number;

    @Column(name = "R")
    private Double how_Many_One_Infects;

    @Column(name = "M")
    private Double mortality_Rate;

    @Column(name = "Ti")
    private Double number_Of_Days_To_Recovery;

    @Column(name = "Tm")
    private Double number_Of_Days_To_Death;

    @Column(name = "Ts")
    private Double simulation_Time;

    @OneToMany(mappedBy = "simulation", cascade = CascadeType.REMOVE)
    private List<SimulationsValues> simulationsValues;
}

SimulationValues:

package Simulations.Entity;

import lombok.*;

import javax.persistence.*;

@Builder
@Table
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class SimulationsValues {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column
    private Long id;

    @Column(name = "Day")
    private Double day;

    @Column(name = "Pi")
    private Double number_Of_Infected;

    @Column(name = "Pv")
    private Double healthy_Prone_To_Infection;

    @Column(name = "Pm")
    private Double dead;

    @Column(name = "Pr")
    private Double regained_Health_And_Immunity;

    @ManyToOne
    @JoinColumn(name = "simulation_id", referencedColumnName = "id")
    private Simulation simulation;
}

逻辑类:

package Simulations.Services;

import Simulations.Entity.Simulation;
import Simulations.Entity.SimulationsValues;
import Simulations.Repositories.SimulationRepository;
import Simulations.Repositories.SimulationsValuesRepository;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Component
@Aspect
public class EachRemainingDayService {


    private final SimulationRepository simulationRepository;
    private final SimulationsValuesRepository simulationsValuesRepository;


    @Pointcut("@annotation(Simulations.Annotations.MyAnnotation)")
    public void MyAnnotationMethod() {
    }


    public List<Simulation> getSimulations() {
        List<Simulation> listOdIds = new ArrayList<>(simulationRepository.findAll());
        listOdIds.sort(Comparator.comparing(Simulation::getId));
        return listOdIds;
    }

    public List<SimulationsValues> getSimulationsValues() {
        List<SimulationsValues> listOdIdsOfValues = new ArrayList<>(simulationsValuesRepository.findAll());
        listOdIdsOfValues.sort(Comparator.comparing(SimulationsValues::getId));
        return listOdIdsOfValues;
    }


    //3 zapis wynikow do bazy
    @After("MyAnnotationMethod()")// zmienilem z Before
    public void SimulationParametersForRemainingDays() {


        var sim = getSimulations();
        var newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());

        var simVal = getSimulationsValues();
        var newestSimulationsVal = simulationsValuesRepository
                .getById(simVal.get(simVal.size() - 1).getId());


        //zapisanie do bazy TO LOGIKA!!!
        for (double i = 2; i <= newestSimulation.getSimulation_Time(); i++) {
            if (newestSimulationsVal.getHealthy_Prone_To_Infection() <= 0) {
                break;
            }

            if (i >= newestSimulation.getNumber_Of_Days_To_Death()
                    && i < newestSimulation.getNumber_Of_Days_To_Recovery()) {

                sim = getSimulations();
                newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());

                simVal = getSimulationsValues();
                newestSimulationsVal = simulationsValuesRepository
                        .getById(simVal.get(simVal.size() - 1).getId());

                SimulationsValues simulation_values;
                Simulation finalNewestSimulation1 = newestSimulation;
                double finalI = i;



                simulation_values = SimulationsValues.builder()
                        .day(i)
                        .healthy_Prone_To_Infection(newestSimulation.getPopulation_Size() -
                                (((newestSimulation.getHow_Many_One_Infects())
                                        * newestSimulationsVal.getNumber_Of_Infected()
                                        + newestSimulationsVal.getNumber_Of_Infected())))


                        .number_Of_Infected((((newestSimulation.getHow_Many_One_Infects())
                                * newestSimulationsVal.getNumber_Of_Infected()
                                + newestSimulationsVal.getNumber_Of_Infected())//L24
                                - (newestSimulation.getMortality_Rate() * (double) ((simulationsValuesRepository
                                .findAll().stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                        .getNumber_Of_Days_To_Death()))
                                .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
                                        .filter(e -> e.getDay() == (finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Death()))).count() - 1).getNumber_Of_Infected()))))//V24


                        .regained_Health_And_Immunity(0d)// Z24


                        .dead((newestSimulation.getMortality_Rate() * (double) ((simulationsValuesRepository
                                .findAll().stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                        .getNumber_Of_Days_To_Death()))
                                .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
                                        .filter(e -> e.getDay() == (finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Death()))).count() - 1).getNumber_Of_Infected()))
                                - newestSimulationsVal.getDead())//V24

                        .build();


                var simId = simulationsValuesRepository.save(simulation_values);
                simId.setSimulation(newestSimulation);//dodane
                var simVal2 = newestSimulation.getSimulationsValues();
                simVal2.add(simId);
                newestSimulation.setSimulationsValues(simVal2);
                simulationRepository.save(newestSimulation);

            } else if (i >= newestSimulation.getNumber_Of_Days_To_Recovery()) {

                sim = getSimulations();
                newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());

                simVal = getSimulationsValues();
                newestSimulationsVal = simulationsValuesRepository
                        .getById(simVal.get(simVal.size() - 1).getId());


                Simulation finalNewestSimulation1 = newestSimulation;
                double finalI = i;
                SimulationsValues simulation_values;


                simulation_values = SimulationsValues.builder()
                        .day(i)
                        .healthy_Prone_To_Infection(newestSimulation.getPopulation_Size() -
                                ((newestSimulation.getHow_Many_One_Infects())
                                        * newestSimulationsVal.getNumber_Of_Infected()))


                        .number_Of_Infected(((newestSimulation.getHow_Many_One_Infects())
                                * newestSimulationsVal.getNumber_Of_Infected()
                                + newestSimulationsVal.getNumber_Of_Infected())//L24

                                - (newestSimulation.getMortality_Rate() * (double) ((simulationsValuesRepository
                                .findAll().stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                        .getNumber_Of_Days_To_Death()))
                                .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
                                        .filter(e -> e.getDay() == (finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Death()))).count() - 1).getNumber_Of_Infected())//V24

                                - (((1 - newestSimulation.getMortality_Rate()) * (simulationsValuesRepository
                                .findAll().stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                        .getNumber_Of_Days_To_Recovery()))
                                .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
                                        .filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Recovery())).count() - 1)
                                .getNumber_Of_Infected()))//Z24
                        ))


                        .regained_Health_And_Immunity((1 - newestSimulation.getMortality_Rate()) *
                                (simulationsValuesRepository.findAll()
                                        .stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Recovery()))
                                        .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll()
                                        .stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Recovery())).count() - 1)
                                        .getNumber_Of_Infected())// Z24


                        .dead((newestSimulation.getMortality_Rate() * (double) ((simulationsValuesRepository.findAll()
                                .stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                        .getNumber_Of_Days_To_Death()))
                                .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
                                        .filter(e -> e.getDay() == (finalI + 1 - finalNewestSimulation1
                                             .getNumber_Of_Days_To_Death()))).count()1).getNumber_Of_Infected()))
                                - newestSimulationsVal.getDead())//V24

                        .build();


                var simId = simulationsValuesRepository.save(simulation_values);
                simId.setSimulation(newestSimulation);//dodane
                var simVal2 = newestSimulation.getSimulationsValues();
                simVal2.add(simId);
                newestSimulation.setSimulationsValues(simVal2);
                simulationRepository.save(newestSimulation);

            } else if (i == 2) {
                sim = getSimulations();
                newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());

                simVal = getSimulationsValues();
                newestSimulationsVal = simulationsValuesRepository
                        .getById(simVal.get(simVal.size() - 1).getId());


                SimulationsValues simulation_values;
                simulation_values = SimulationsValues.builder()
                        .day(i)
                        .healthy_Prone_To_Infection(newestSimulationsVal.getHealthy_Prone_To_Infection() -
                                ((newestSimulation.getHow_Many_One_Infects())
                                        * newestSimulationsVal.getNumber_Of_Infected()))
                        .number_Of_Infected((newestSimulation.getHow_Many_One_Infects())
                                * newestSimulationsVal.getNumber_Of_Infected()
                                + newestSimulationsVal.getNumber_Of_Infected())
                        .regained_Health_And_Immunity(0d)
                        .dead(0d)
                        .build();

                var simId2 = simulationsValuesRepository.save(simulation_values);
                simId2.setSimulation(newestSimulation);//dodane
                var simVal2 = newestSimulation.getSimulationsValues();
                simVal2.add(simId2);
                newestSimulation.setSimulationsValues(simVal2);
                simulationRepository.save(newestSimulation);

            } else {
                sim = getSimulations();
                newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());

                simVal = getSimulationsValues();
                newestSimulationsVal = simulationsValuesRepository
                        .getById(simVal.get(simVal.size() - 1).getId());


                SimulationsValues simulation_values;
                simulation_values = SimulationsValues.builder()
                        .day(i)
                        .healthy_Prone_To_Infection(newestSimulationsVal.getHealthy_Prone_To_Infection() -
                                ((newestSimulation.getHow_Many_One_Infects())
                                        * newestSimulationsVal.getNumber_Of_Infected()))
                        .number_Of_Infected((newestSimulation.getHow_Many_One_Infects())
                                * newestSimulationsVal.getNumber_Of_Infected()
                                + newestSimulationsVal.getNumber_Of_Infected())
                        .regained_Health_And_Immunity(0d)
                        .dead(0d)
                        .build();

                var simId2 = simulationsValuesRepository.save(simulation_values);
                simId2.setSimulation(newestSimulation);//dodane
                var simVal2 = newestSimulation.getSimulationsValues();
                simVal2.add(simId2);
                newestSimulation.setSimulationsValues(simVal2);
                simulationRepository.save(newestSimulation);

            }
        }

    }
}


ViewModel:


package Simulations.ViewModels;

import Simulations.Annotations.GreaterThan;
import Simulations.Entity.SimulationsValues;
import lombok.*;

import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@GreaterThan(message = "field number of days to death can not be equal or bigger than field days to recovery")
public class SimulationViewModel {

    private Long id;

    @NotEmpty(message = "field can not be empty")
    private String simulation_Name;

    @NotNull(message = "field can not be empty")
    @DecimalMax("10000000000.0")
    @DecimalMin("0.0")
    private Double population_Size;

    private Double initial_Infected_Number;

    @NotNull(message = "field can not be empty")
    private Double how_Many_One_Infects;

    @NotNull(message = "field can not be empty")
    @DecimalMax("1.0")
    @DecimalMin("0.001")
    private Double mortality_Rate;

    private @NotNull(message = "field can not be empty") @DecimalMin("1.0")
    Double number_Of_Days_To_Recovery;

    private @NotNull(message = "field can not be empty") @DecimalMin("1.0")
    Double number_Of_Days_To_Death;

    private @NotNull(message = "field can not be empty")
    Double simulation_Time;

    List<SimulationsValues> simulationsValues = new ArrayList<>();
}

嗨,我有一个问题。当我创建一个simulationValues对象(使用构建器)时,它的字段具有预期的双值,但是当我将此对象保存到数据库并查看数据库(MySql)中的这些值时,这些值显示为整数,即逗号后没有值,例如当我调试我的代码,发现“dead”字段在 12 次迭代中值为 0.9,但它保存到数据库时为 1。这是为什么?我不进行任何转换,我用于此计算的所有字段都是双精度数,并且所有 SimulationValues 类和SimulationViewModel 字段都是双精度数。为什么保存字段时会更改类型?提前致谢

Entities:
Simulation:

package Simulations.Entity;

import lombok.*;

import javax.persistence.*;
import java.util.List;

@Builder
@Table
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Simulation {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column
    private Long id;

    @Column(name = "N")
    private String simulation_Name;

    @Column(name = "P")
    private Double population_Size;

    @Column(name = "I")
    private Double initial_Infected_Number;

    @Column(name = "R")
    private Double how_Many_One_Infects;

    @Column(name = "M")
    private Double mortality_Rate;

    @Column(name = "Ti")
    private Double number_Of_Days_To_Recovery;

    @Column(name = "Tm")
    private Double number_Of_Days_To_Death;

    @Column(name = "Ts")
    private Double simulation_Time;

    @OneToMany(mappedBy = "simulation", cascade = CascadeType.REMOVE)
    private List<SimulationsValues> simulationsValues;
}

SimulationValues:

package Simulations.Entity;

import lombok.*;

import javax.persistence.*;

@Builder
@Table
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class SimulationsValues {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column
    private Long id;

    @Column(name = "Day")
    private Double day;

    @Column(name = "Pi")
    private Double number_Of_Infected;

    @Column(name = "Pv")
    private Double healthy_Prone_To_Infection;

    @Column(name = "Pm")
    private Double dead;

    @Column(name = "Pr")
    private Double regained_Health_And_Immunity;

    @ManyToOne
    @JoinColumn(name = "simulation_id", referencedColumnName = "id")
    private Simulation simulation;
}

Logic calss:

package Simulations.Services;

import Simulations.Entity.Simulation;
import Simulations.Entity.SimulationsValues;
import Simulations.Repositories.SimulationRepository;
import Simulations.Repositories.SimulationsValuesRepository;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Component
@Aspect
public class EachRemainingDayService {


    private final SimulationRepository simulationRepository;
    private final SimulationsValuesRepository simulationsValuesRepository;


    @Pointcut("@annotation(Simulations.Annotations.MyAnnotation)")
    public void MyAnnotationMethod() {
    }


    public List<Simulation> getSimulations() {
        List<Simulation> listOdIds = new ArrayList<>(simulationRepository.findAll());
        listOdIds.sort(Comparator.comparing(Simulation::getId));
        return listOdIds;
    }

    public List<SimulationsValues> getSimulationsValues() {
        List<SimulationsValues> listOdIdsOfValues = new ArrayList<>(simulationsValuesRepository.findAll());
        listOdIdsOfValues.sort(Comparator.comparing(SimulationsValues::getId));
        return listOdIdsOfValues;
    }


    //3 zapis wynikow do bazy
    @After("MyAnnotationMethod()")// zmienilem z Before
    public void SimulationParametersForRemainingDays() {


        var sim = getSimulations();
        var newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());

        var simVal = getSimulationsValues();
        var newestSimulationsVal = simulationsValuesRepository
                .getById(simVal.get(simVal.size() - 1).getId());


        //zapisanie do bazy TO LOGIKA!!!
        for (double i = 2; i <= newestSimulation.getSimulation_Time(); i++) {
            if (newestSimulationsVal.getHealthy_Prone_To_Infection() <= 0) {
                break;
            }

            if (i >= newestSimulation.getNumber_Of_Days_To_Death()
                    && i < newestSimulation.getNumber_Of_Days_To_Recovery()) {

                sim = getSimulations();
                newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());

                simVal = getSimulationsValues();
                newestSimulationsVal = simulationsValuesRepository
                        .getById(simVal.get(simVal.size() - 1).getId());

                SimulationsValues simulation_values;
                Simulation finalNewestSimulation1 = newestSimulation;
                double finalI = i;



                simulation_values = SimulationsValues.builder()
                        .day(i)
                        .healthy_Prone_To_Infection(newestSimulation.getPopulation_Size() -
                                (((newestSimulation.getHow_Many_One_Infects())
                                        * newestSimulationsVal.getNumber_Of_Infected()
                                        + newestSimulationsVal.getNumber_Of_Infected())))


                        .number_Of_Infected((((newestSimulation.getHow_Many_One_Infects())
                                * newestSimulationsVal.getNumber_Of_Infected()
                                + newestSimulationsVal.getNumber_Of_Infected())//L24
                                - (newestSimulation.getMortality_Rate() * (double) ((simulationsValuesRepository
                                .findAll().stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                        .getNumber_Of_Days_To_Death()))
                                .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
                                        .filter(e -> e.getDay() == (finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Death()))).count() - 1).getNumber_Of_Infected()))))//V24


                        .regained_Health_And_Immunity(0d)// Z24


                        .dead((newestSimulation.getMortality_Rate() * (double) ((simulationsValuesRepository
                                .findAll().stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                        .getNumber_Of_Days_To_Death()))
                                .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
                                        .filter(e -> e.getDay() == (finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Death()))).count() - 1).getNumber_Of_Infected()))
                                - newestSimulationsVal.getDead())//V24

                        .build();


                var simId = simulationsValuesRepository.save(simulation_values);
                simId.setSimulation(newestSimulation);//dodane
                var simVal2 = newestSimulation.getSimulationsValues();
                simVal2.add(simId);
                newestSimulation.setSimulationsValues(simVal2);
                simulationRepository.save(newestSimulation);

            } else if (i >= newestSimulation.getNumber_Of_Days_To_Recovery()) {

                sim = getSimulations();
                newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());

                simVal = getSimulationsValues();
                newestSimulationsVal = simulationsValuesRepository
                        .getById(simVal.get(simVal.size() - 1).getId());


                Simulation finalNewestSimulation1 = newestSimulation;
                double finalI = i;
                SimulationsValues simulation_values;


                simulation_values = SimulationsValues.builder()
                        .day(i)
                        .healthy_Prone_To_Infection(newestSimulation.getPopulation_Size() -
                                ((newestSimulation.getHow_Many_One_Infects())
                                        * newestSimulationsVal.getNumber_Of_Infected()))


                        .number_Of_Infected(((newestSimulation.getHow_Many_One_Infects())
                                * newestSimulationsVal.getNumber_Of_Infected()
                                + newestSimulationsVal.getNumber_Of_Infected())//L24

                                - (newestSimulation.getMortality_Rate() * (double) ((simulationsValuesRepository
                                .findAll().stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                        .getNumber_Of_Days_To_Death()))
                                .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
                                        .filter(e -> e.getDay() == (finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Death()))).count() - 1).getNumber_Of_Infected())//V24

                                - (((1 - newestSimulation.getMortality_Rate()) * (simulationsValuesRepository
                                .findAll().stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                        .getNumber_Of_Days_To_Recovery()))
                                .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
                                        .filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Recovery())).count() - 1)
                                .getNumber_Of_Infected()))//Z24
                        ))


                        .regained_Health_And_Immunity((1 - newestSimulation.getMortality_Rate()) *
                                (simulationsValuesRepository.findAll()
                                        .stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Recovery()))
                                        .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll()
                                        .stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Recovery())).count() - 1)
                                        .getNumber_Of_Infected())// Z24


                        .dead((newestSimulation.getMortality_Rate() * (double) ((simulationsValuesRepository.findAll()
                                .stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                        .getNumber_Of_Days_To_Death()))
                                .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
                                        .filter(e -> e.getDay() == (finalI + 1 - finalNewestSimulation1
                                             .getNumber_Of_Days_To_Death()))).count()1).getNumber_Of_Infected()))
                                - newestSimulationsVal.getDead())//V24

                        .build();


                var simId = simulationsValuesRepository.save(simulation_values);
                simId.setSimulation(newestSimulation);//dodane
                var simVal2 = newestSimulation.getSimulationsValues();
                simVal2.add(simId);
                newestSimulation.setSimulationsValues(simVal2);
                simulationRepository.save(newestSimulation);

            } else if (i == 2) {
                sim = getSimulations();
                newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());

                simVal = getSimulationsValues();
                newestSimulationsVal = simulationsValuesRepository
                        .getById(simVal.get(simVal.size() - 1).getId());


                SimulationsValues simulation_values;
                simulation_values = SimulationsValues.builder()
                        .day(i)
                        .healthy_Prone_To_Infection(newestSimulationsVal.getHealthy_Prone_To_Infection() -
                                ((newestSimulation.getHow_Many_One_Infects())
                                        * newestSimulationsVal.getNumber_Of_Infected()))
                        .number_Of_Infected((newestSimulation.getHow_Many_One_Infects())
                                * newestSimulationsVal.getNumber_Of_Infected()
                                + newestSimulationsVal.getNumber_Of_Infected())
                        .regained_Health_And_Immunity(0d)
                        .dead(0d)
                        .build();

                var simId2 = simulationsValuesRepository.save(simulation_values);
                simId2.setSimulation(newestSimulation);//dodane
                var simVal2 = newestSimulation.getSimulationsValues();
                simVal2.add(simId2);
                newestSimulation.setSimulationsValues(simVal2);
                simulationRepository.save(newestSimulation);

            } else {
                sim = getSimulations();
                newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());

                simVal = getSimulationsValues();
                newestSimulationsVal = simulationsValuesRepository
                        .getById(simVal.get(simVal.size() - 1).getId());


                SimulationsValues simulation_values;
                simulation_values = SimulationsValues.builder()
                        .day(i)
                        .healthy_Prone_To_Infection(newestSimulationsVal.getHealthy_Prone_To_Infection() -
                                ((newestSimulation.getHow_Many_One_Infects())
                                        * newestSimulationsVal.getNumber_Of_Infected()))
                        .number_Of_Infected((newestSimulation.getHow_Many_One_Infects())
                                * newestSimulationsVal.getNumber_Of_Infected()
                                + newestSimulationsVal.getNumber_Of_Infected())
                        .regained_Health_And_Immunity(0d)
                        .dead(0d)
                        .build();

                var simId2 = simulationsValuesRepository.save(simulation_values);
                simId2.setSimulation(newestSimulation);//dodane
                var simVal2 = newestSimulation.getSimulationsValues();
                simVal2.add(simId2);
                newestSimulation.setSimulationsValues(simVal2);
                simulationRepository.save(newestSimulation);

            }
        }

    }
}


ViewModel:


package Simulations.ViewModels;

import Simulations.Annotations.GreaterThan;
import Simulations.Entity.SimulationsValues;
import lombok.*;

import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@GreaterThan(message = "field number of days to death can not be equal or bigger than field days to recovery")
public class SimulationViewModel {

    private Long id;

    @NotEmpty(message = "field can not be empty")
    private String simulation_Name;

    @NotNull(message = "field can not be empty")
    @DecimalMax("10000000000.0")
    @DecimalMin("0.0")
    private Double population_Size;

    private Double initial_Infected_Number;

    @NotNull(message = "field can not be empty")
    private Double how_Many_One_Infects;

    @NotNull(message = "field can not be empty")
    @DecimalMax("1.0")
    @DecimalMin("0.001")
    private Double mortality_Rate;

    private @NotNull(message = "field can not be empty") @DecimalMin("1.0")
    Double number_Of_Days_To_Recovery;

    private @NotNull(message = "field can not be empty") @DecimalMin("1.0")
    Double number_Of_Days_To_Death;

    private @NotNull(message = "field can not be empty")
    Double simulation_Time;

    List<SimulationsValues> simulationsValues = new ArrayList<>();
}

Hi, I have a roblem. When I create a simulationValues object (using builder) it's fields have double values as intended but when I save this object to database and look at thoese values in database (MySql) thoese values are presented as Integers i.e. without values after comma, for example when I debug my code I see that "dead" field has in 12 iteration value 0.9 but it is saved to database as 1. Why is that? I don;t do any casting and all I use for this calculations are doubles as well as all fields n SimulationValues class and SimulationViewModel are doubles. Why when saving field changes types?Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

MySql 中的所有列都是 bigint 类型而不是 double,因为我在相应的实体中制作了它们,将它们更改为 BigDecimal,一切正常。

All columns in MySql were bigint type instead of double as I made them in coresponding entity, changed them to BigDecimal and everything works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文