title: Spring: How to Extract Data from One Object and Insert Into Another
body: <p>I currently have two tables: <strong>Workforce</strong> and <strong>Employees</strong>. The <strong>Workforce</strong> table is a collection of all potential employees who have submitted resumes to join the company. The <strong>Employees</strong> table is a collection of persons in the <strong>Workforce</strong> table that have actually been hired.</p>
  
  <p><strong>Environment</strong>:
  1. Eclipse
  2. Java (J2EE)
  3. Maven
  4. Spring
  5. JPA (JpaRepository)</p>
  
  <p>For brevity, I will stick to the files that are of concern for this extraction of data and insertion of data into another table.</p>
  
  <p>Because of the similarities of both tables, many fields in the <strong>Workforce</strong> table will be similar to those in the <strong>Employees</strong> table:</p>
  
  <ol>
  <li>First Name</li>
  <li>Last Name</li>
  <li>Department</li>
  <li>Phone Number
  etc...</li>
  </ol>
  
  <p>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 <strong>Workforce</strong> table with values of "1" for "isHired" will be extracted from the Workforce table and inserted into the Employees table.</p>
  
  <p>The general SQL Query looks something like this (and works in a regular IDE with no Spring HPQL constraints):</p>
  
  <pre><code>INSERT INTO Employees (firstName, lastName, dept, phone) 
  SELECT emp_firstName, emp_lastName, emp_dept, emp_phone 
  FROM Workforce WHERE isHired = 1
  </code></pre>
  
  <p>This works without a hitch, but we're talking about JPA, so this is what I have done:</p>
  
  <p>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)</p>
  
  <p><strong>WorkforceRepository.java</strong></p>
  
  <pre><code>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&lt;Workforce, Long&gt; {
      @Query("SELECT new Workforce("
          + "w.workforceid, "
          + "w.firstName, "
          + "w.lastName, "
          + "w.dept, "
          + "w.phone) "
          + "FROM Workforce w WHERE isHired = 1")
      public List&lt;Workforce&gt; getNewHires();
  
  }
  </code></pre>
  
  <p><strong>Workforce.java</strong></p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p><strong>NOTE: I have left out the Employee.java and EmployeesRepository.java for BREVITY</strong> They are pretty much the same and aside from the <strong>employeeid</strong>, 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.</p>
  
  <p>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:</p>
  
  <p><strong>EmployeesController.java</strong></p>
  
  <pre><code>// IMPORTS REMOVED FOR BREVITY
  List&lt;Workforce&gt; retrievedRisks = Workforce.getNewHires();
  for (Workforce w: retrievedRisks)
      Employees.saveAndFlush(w);
  // MISC CODE REMOVED FOR BREVITY
  </code></pre>
  
  <p>The error I receive of course is below:</p>
  
  <pre><code>Type mismatch: cannot convert from element type Workforce to Employees
  </code></pre>
  
  <p>Anyone know how to extract data from one POJO object and insert it into another using JpaRepository?</p>