2017-10-20 109 views
0

我需要更新我的表內的兩個列(Job此表與其他兩個表員工和工作歷史聯合)其中一個是主鍵,但我得到錯誤,如果有人可以幫忙。更新彈簧引導實體管理器的主鍵jpa

package com.touati.org.model; 
import java.io.Serializable; 
import javax.persistence.*; 
import java.math.BigDecimal; 
import java.util.List; 


/** 
* The persistent class for the jobs database table. 
* 
*/ 
@Entity 
@Table(name="jobs") 
@NamedQuery(name="Job.findAll", query="SELECT j FROM Job j") 
public class Job implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    @Column(name="JOB_ID") 
    private String jobId; 

    @Column(name="JOB_TITLE") 
    private String jobTitle; 

    @Column(name="MAX_SALARY") 
    private BigDecimal maxSalary; 

    @Column(name="MIN_SALARY") 
    private BigDecimal minSalary; 

    //bi-directional many-to-one association to Employee 
    @OneToMany(mappedBy="job") 
    private List<Employee> employees; 

    //bi-directional many-to-one association to JobHistory 
    @OneToMany(mappedBy="job") 
    private List<JobHistory> jobHistories; 

    public Job() { 
    } 

    public String getJobId() { 
     return this.jobId; 
    } 

    public void setJobId(String jobId) { 
     this.jobId = jobId; 
    } 

    public String getJobTitle() { 
     return this.jobTitle; 
    } 

    public void setJobTitle(String jobTitle) { 
     this.jobTitle = jobTitle; 
    } 

    public BigDecimal getMaxSalary() { 
     return this.maxSalary; 
    } 

    public void setMaxSalary(BigDecimal maxSalary) { 
     this.maxSalary = maxSalary; 
    } 

    public BigDecimal getMinSalary() { 
     return this.minSalary; 
    } 

    public void setMinSalary(BigDecimal minSalary) { 
     this.minSalary = minSalary; 
    } 

    public List<Employee> getEmployees() { 
     return this.employees; 
    } 

    public void setEmployees(List<Employee> employees) { 
     this.employees = employees; 
    } 

    public Employee addEmployee(Employee employee) { 
     getEmployees().add(employee); 
     employee.setJob(this); 

     return employee; 
    } 

    public Employee removeEmployee(Employee employee) { 
     getEmployees().remove(employee); 
     employee.setJob(null); 

     return employee; 
    } 

    public List<JobHistory> getJobHistories() { 
     return this.jobHistories; 
    } 

    public void setJobHistories(List<JobHistory> jobHistories) { 
     this.jobHistories = jobHistories; 
    } 

    public JobHistory addJobHistory(JobHistory jobHistory) { 
     getJobHistories().add(jobHistory); 
     jobHistory.setJob(this); 

     return jobHistory; 
    } 

    public JobHistory removeJobHistory(JobHistory jobHistory) { 
     getJobHistories().remove(jobHistory); 
     jobHistory.setJob(null); 

     return jobHistory; 
    } 

} 

我的控制器:這裏的時候,我試圖尋找在數據庫能正常工作,另外,如果我嘗試中庸之道更新它工作正常作業的標題,但如果我嘗試將所有的工作爲作業表設置一個新的主鍵,它給了我錯誤在這裏我的控制器。

package com.touati.org.model; 

import java.io.IOException; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.web.bind.annotation.ResponseStatus; 







@Controller // This means that this class is a Controller 
@RequestMapping(path="/project") // This means URL's start with /demo (after Application path) 
public class MainController { 



    @GetMapping(path="/job") 
    public @ResponseBody Iterable<Job> getAllJob() { 
     // This returns a JSON or XML with the users 
     return jobRepository.findAll(); 
    } 




    @GetMapping(path="/job/{jobId}") 
    public @ResponseBody String getJob(@PathVariable String jobId) { 
     Job job = jobRepository.findOne(jobId); 

     try { 
     job.setJobTitle("manager"); 
     job.setJobId("test1"); 
     jobRepository.save(job); 
     } 
     catch (Exception ex) { 
      return "Error updating the job: " + ex.toString(); 
     } 
     return "Job succesfully updated!"; 



    } 

} 

我得到這個錯誤,

Error updating the user: org.springframework.orm.jpa.JpaSystemException: identifier of an instance of com.touati.org.model.Job was altered from test to test1; nested exception is org.hibernate.HibernateException: identifier of an instance of com.touati.org.model.Job was altered from test to test1 

謝謝您的幫助。

+1

爲什麼你會改變你的對象的主鍵? – Romulo

+0

好吧,只要閱讀錯誤信息即可。它確切地告訴你你做錯了什麼。您正在將Job實體的ID更改爲test1。您不得更改實體的ID。它不變地識別實體。也很不清楚爲什麼名爲getJob()的方法,使用GET請求調用,修改作業,並返回「用戶成功更新!」。這真的沒有意義。 –

+0

我需要修改此表的主鍵以查看其他表作爲外鍵的影響 –

回答

1

主鍵不應該改變。如果你需要改變主鍵,這意味着你的設計不好。如果您需要經常更改JOB_ID,請爲ID等主鍵創建另一列。另一種可能性是複製所有屬性並使用新的JOB_ID創建新記錄,然後刪除舊記錄。