简体   繁体   中英

Spring: How to Extract Data from One Object and Insert Into Another

I currently have two tables: Workforce and Employees . The Workforce table is a collection of all potential employees who have submitted resumes to join the company. The Employees table is a collection of persons in the Workforce table that have actually been hired.

Environment : 1. Eclipse 2. Java (J2EE) 3. Maven 4. Spring 5. JPA (JpaRepository)

For brevity, I will stick to the files that are of concern for this extraction of data and insertion of data into another table.

Because of the similarities of both tables, many fields in the Workforce table will be similar to those in the Employees table:

  1. First Name
  2. Last Name
  3. Department
  4. Phone Number etc...

If the person in the workforce is chosen, there is a boolean value called "isHired" that will have the value of 1. Every week, the person's in the Workforce table with values of "1" for "isHired" will be extracted from the Workforce table and inserted into the Employees table.

The general SQL Query looks something like this (and works in a regular IDE with no Spring HPQL constraints):

INSERT INTO Employees (firstName, lastName, dept, phone) 
SELECT emp_firstName, emp_lastName, emp_dept, emp_phone 
FROM Workforce WHERE isHired = 1

This works without a hitch, but we're talking about JPA, so this is what I have done:

In a Repository, the process has been broken into two items: 1) extracting data from one table (Workforce) and then 2) inserting extracted data into another table (Employees)

WorkforceRepository.java

package com.example.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.example.entities.Workforce;

@Repository
public interface WorkforceRepository extends JpaRepository<Workforce, Long> {
    @Query("SELECT new Workforce("
        + "w.workforceid, "
        + "w.firstName, "
        + "w.lastName, "
        + "w.dept, "
        + "w.phone) "
        + "FROM Workforce w WHERE isHired = 1")
    public List<Workforce> getNewHires();

}

Workforce.java

package com.example.entities;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Generatedvalue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table

@Entity
@Table(name = "WORKFORCE")
public class Workforce implements Serializable {
    private static final long serialVersionUID = 1L;

    public Workforce() {
    }
    public Workforce(long workforceid, String firstName, lastName, dept, phone) {
    this.workforceid = workforceid;
    this.firstName = firstName;
    this.lastName = lastName;
    this.dept = dept;
    this.phone = phone;
    }
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="workforceid")
    private long workforceid;

    @Column(name="firstName)
    private String firstName;
    // OTHER DECLARATIONS REMOVED FOR BREVITY
    // GETTERS AND SETTERS REMOVED FOR BREVITY
}

NOTE: I have left out the Employee.java and EmployeesRepository.java for BREVITY They are pretty much the same and aside from the employeeid , that is the primary key for the Employee table, the other columns are exactly the same. The contructor class only contains the four columns that are the same as well.

I have a ton of CRUD operations that work, while the operations are only occurring within the same table. However, I would like to do this:

EmployeesController.java

// IMPORTS REMOVED FOR BREVITY
List<Workforce> retrievedRisks = Workforce.getNewHires();
for (Workforce w: retrievedRisks)
    Employees.saveAndFlush(w);
// MISC CODE REMOVED FOR BREVITY

The error I receive of course is below:

Type mismatch: cannot convert from element type Workforce to Employees

Anyone know how to extract data from one POJO object and insert it into another using JpaRepository?

If Employees and Workforce almoust the same you can use hibernate inheritance . Add copy constructor and resave entity.But it might not to help if you have different constraints or id generator (might be a case when Employees already exists with Workforce ids ). Simple case it's just create Employees from Workforce and save created object via

employee = new Employees(w.firstName, w.lastName, w.dept, w.phone);
Employees.saveAndFlush(employee);

in this case you create new Employees and repository saves it correct. Maybe after save newEmployees (copy of Workforce ) you should delete old Workforce

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