简体   繁体   中英

trying to read and write json data into psotgres database from Spring Boot

Hello i am trying to add data from a json file to my local Postgres database but no luck. i keep getting this error, i created the employee.json but still no luck with getting it running, can anyone assit with this. i followed the tutorial found on https://www.danvega.dev/blog/2017/07/05/read-json-data-spring-boot-write-database/ *

Caused by: org.postgresql.util.PSQLException: ERROR: insert or update on table "employee" violates foreign key constraint "department_id"
  Detail: Key (department_id)=(1) is not present in table "department".
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2532) ~[postgresql-42.2.14.jar:42.2.14]
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2267) ~[postgresql-42.2.14.jar:42.2.14]
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:312) ~[postgresql-42.2.14.jar:42.2.14]
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:448) ~[postgresql-42.2.14.jar:42.2.14]
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:369) ~[postgresql-42.2.14.jar:42.2.14]
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:153) ~[postgresql-42.2.14.jar:42.2.14]
    at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:119) ~[postgresql-42.2.14.jar:42.2.14]
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.4.5.jar:na]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
    ... 78 common frames omitted

HERES MY CODE

EMPLOYEE.JAVA

Table
@Entity
public class Employee implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "employee_id")
    @NotNull
    private Long id;

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="department_id")
    @NotNull
    private Long department;

    @Column(name="team_id")
    @NotNull
    private Long team;

    @Column(name="policy_id")
    @NotNull
    private Long policy;

    @Column(name = "first_name")
    @NotNull
    private String firstName;

    @Column(name = "last_name")
    @NotNull
    private String lastName;

    @Column(name = "GMID")
    @NotNull
    private String GMID;

    @Column(name = "GMIN", nullable = false)
    @NotNull
    private Long GMIN;

    @Column(name = "policy_overrides")
    @NotNull
    private String policyOverrides;

    @Column(name = "holiday_details")
    @NotNull
    private String holidayDetails;


    @JsonDeserialize(using = LocalDateDeserializer.class)
    @JsonSerialize(using = LocalDateSerializer.class)
    @JsonFormat(pattern="yyyy-MM-dd")
    @Column(name = "date_started")
    @NotNull
    private LocalDate dateStarted;

    @Column(name = "email_address")
    @NotNull
    private String emailAddress;

    public Employee() {
    }

    public Employee(Long department, Long team, Long policy, String firstName, String lastName, String GMID, Long GMIN, String policyOverrides, String holidayDetails, LocalDate dateStarted, String emailAddress) {
        this.department = department;
        this.team = team;
        this.policy = policy;
        this.firstName = firstName;
        this.lastName = lastName;
        this.GMID = GMID;
        this.GMIN = GMIN;
        this.policyOverrides = policyOverrides;
        this.holidayDetails = holidayDetails;
        this.dateStarted = dateStarted;
        this.emailAddress = emailAddress;
    }


    // For updating existing column in database
    public Employee(Long id, Long department, Long team, Long policy, String firstName, String lastName, String GMID, Long GMIN, String policyOverrides, String holidayDetails, LocalDate dateStarted, String emailAddress){
        this.id = id;
        this.department = department;
        this.team = team;
        this.policy = policy;
        this.firstName = firstName;
        this.lastName = lastName;
        this.GMID = GMID;
        this.GMIN = GMIN;
        this.policyOverrides = policyOverrides;
        this.holidayDetails = holidayDetails;
        this.dateStarted = dateStarted;
        this.emailAddress = emailAddress;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getDepartment() {
        return department;
    }

    public void setDepartment(Long department) {
        this.department = department;
    }

    public Long getTeam() {
        return team;
    }

    public void setTeam(Long team) {
        this.team = team;
    }

    public Long getPolicy() {
        return policy;
    }

    public void setPolicy(Long policy) {
        this.policy = policy;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getGMID() {
        return GMID;
    }

    public void setGMID(String GMID) {
        this.GMID = GMID;
    }

    public Long getGMIN() {
        return GMIN;
    }

    public void setGMIN(Long GMIN) {
        this.GMIN = GMIN;
    }

    public String getHolidayDetails() {
        return holidayDetails;
    }

    public void setHolidayDetails(String holidayDetails) {
        this.holidayDetails = holidayDetails;
    }

    public LocalDate getDateStarted() {
        return dateStarted;
    }

    public void setDateStarted(LocalDate date_started) {
        this.dateStarted = date_started;
    }

    public String getPolicyOverrides() {
        return policyOverrides;
    }

    public void setPolicyOverrides(String policyOverrides) {
        this.policyOverrides = policyOverrides;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    @Override
    public String toString() {
        return "{" +
                "\"id\":" + id +
                ", \"department\":" + department +
                ", \"team\":" + team +
                ", \"policy\":" + policy +
                ", \"firstName\":\"" + firstName + "\"" +
                ", \"lastName\":\"" + lastName + "\"" +
                ", \"email\":\"" + emailAddress + "\"" +
                ", \"GMID\":\"" + GMID + "\"" +
                ", \"GMIN\":" + GMIN + "" +
                ", \"policyOverrides\":\"" + policyOverrides + "\"" +
                ", \"holidayDetails\":\"" + holidayDetails + "\"" +
                ", \"dateStarted\":\"" + dateStarted + "\"" +
                '}';
    }

}

DEPARTMENT.JAVA

package com.gm.absencetracker.models;

import com.fasterxml.jackson.annotation.JsonProperty;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

@Table
@Entity
public class Department implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "department_id")
    @NotNull
    @JsonProperty("id")
    private Long id;


    @Column(name = "department_name")
    @NotNull
    @JsonProperty("departmentName")
    private String departmentName;


    @Column(name = "first_manager")
    @NotNull
    @JsonProperty("firstManager")
    private String firstManager;

    
    @Column(name = "alternative_manager")
    @NotNull
    @JsonProperty("alternativeManager")
    private String alternativeManager;

    public Department() {
    }

    public Department(String departmentName, String firstManager, String alternativeManager ) {
        this.departmentName = departmentName;
        this.firstManager = firstManager;
        this.alternativeManager = alternativeManager;
    }

    // For updating existing column in database
    public Department(Long id, String departmentName, String firstManager, String alternativeManager ) {
        this.id = id;
        this.departmentName = departmentName;
        this.firstManager = firstManager;
        this.alternativeManager = alternativeManager;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    public String getFirstManager() {
        return firstManager;
    }

    public void setFirstManager(String firstManager) {
        this.firstManager = firstManager;
    }

    public String getAlternativeManager() {
        return alternativeManager;
    }

    public void setAlternativeManager(String alternativeManager) {
        this.alternativeManager = alternativeManager;
    }

    @Override
    public String toString() {
        return "{" +
                "\"id\":" + id +
                ", \"departmentName\":\"" + departmentName + "\"" +
                ", \"firstManager\":\"" + firstManager + "\"" +
                ", \"alternativeManager\":\"" + alternativeManager + "\"" +
                '}';
    }
}

employee.json

[
    {
      "id": 1,
      "department": 1,
      "team": 1,
      "policy": 1,
      "firstName": "Laxmivaraprasad",
      "lastName": "Ale",
      "gmid": "HZDT3V",
      "gmin": "475609900",
      "policyOverrides": "",
      "holidayDetails":"",
      "dateStarted": "2020-07-13",
      "emailAddress": "laxmivaraprasad.1.ale@gm.com"
    },
    {
      "id": 1,
      "department":1,
      "team": 1,
      "policy": 1,
      "firstName": "Michael",
      "lastName": "Aroyehun",
      "gmid": "KZV0FP",
      "gmin": "246873348",
      "policyOverrides": "",
      "holidayDetails":"",
      "dateStarted": "2020-07-13",
      "emailAddress": "michael.o.aroyehun@gm.com"
    }
]

MainApplication.java

package com.gm.absencetracker;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.gm.absencetracker.models.Employee;
import com.gm.absencetracker.repository.EmployeeRepository;
import com.gm.absencetracker.service.EmployeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.EventListener;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@SpringBootApplication
public class AbsenceTrackerServiceApplication {

    private final static Logger LOGGER = LoggerFactory.getLogger(AbsenceTrackerServiceApplication.class);

    @Autowired
    private FakeDataFactory fakeDataFactory;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private EmployeeRepository employeeRepository;
    @Autowired
    private ObjectMapper objectMapper;

    public static void main(String[] args) {
        LOGGER.info("Application is running");
        SpringApplication.run(AbsenceTrackerServiceApplication.class, args);
    }

    @PostConstruct
    public void setUp() {

        objectMapper.registerModule(new JavaTimeModule());
    }

    @EventListener(ApplicationReadyEvent.class)
    public void addData() {
        fakeDataFactory.addPolicys();
        fakeDataFactory.addDepartments();
        fakeDataFactory.addTeams();
        //fakeDataFactory.addEmployees();
        fakeDataFactory.addHolidays();
    }

    @Bean
    CommandLineRunner runner(EmployeeService employeeService) {
        return args -> {
            // read json and write to db
            ObjectMapper mapper = new ObjectMapper();
            TypeReference<List<Employee>> typeReference = new TypeReference<>() {
            };
            InputStream inputStream = TypeReference.class.getResourceAsStream("/json/employee.json");
            try {
                List<Employee> users = mapper.readValue(inputStream, typeReference);

                employeeService.saveAll(users);

                System.out.println("Users Saved!");
            } catch (IOException e) {
                System.out.println("Unable to save users: " + e.getMessage());
            }
        };

    }
}

TL;DR: addData() runs before the CommandLineRunner . Change event to ApplicationStartedEvent .


If you look at the source code for SpringApplication , you will see the sequence of events.

The sequence of events are:

  1. Event: ApplicationStartingEvent
  2. Logs "Starting ...Application on ..."
  3. Event: ContextRefreshedEvent
  4. Logs "Started ...Application in ... seconds ..."
  5. Event: ApplicationStartedEvent
  6. Executing CommandLineRunner beans
  7. Event: ApplicationReadyEvent
  8. Returns, likely to main()

This means that your addData() runs after the CommandLineRunner .

Solution 1: Change to @EventListener(ApplicationStartedEvent.class) to make it run before the CommandLineRunner .

Solution 2: Move the code that's in the CommandLineRunner to the main() method.

Here is the abbreviated source code for SpringApplication.run() :

public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    // ...
    listeners.starting(); // #1
    try {
        // ...
        Banner printedBanner = printBanner(environment); // #2
        context = createApplicationContext(); // #3
        // ...
        stopWatch.stop();
        if (this.logStartupInfo) { // #4
            new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
        }
        listeners.started(context); // #5
        callRunners(context, applicationArguments); // #6
    }
    catch (Throwable ex) {
        // ...
    }

    try {
        listeners.running(context); // #7
    }
    catch (Throwable ex) {
        // ...
    }
    return context; // #8
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM