首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新主键spring boot jpa

更新主键spring boot jpa
EN

Stack Overflow用户
提问于 2017-10-21 06:05:28
回答 1查看 4.1K关注 0票数 1

我需要更新表中的两列(作业这个表与另外两个表employees和job-history联合),其中一个是主键,但我得到了错误,如果有人可以帮助的话!

代码语言:javascript
复制
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;
    }

}

我的控制器:在这里,当我试图在数据库中查找所有作业时,它工作得很好,而且如果我只更新作业的标题,它工作得很好,但如果我试图为作业表设置一个新的主键,它会给我一个错误,我的控制器。

代码语言:javascript
复制
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!";



    }

我得到了这个错误,

代码语言:javascript
复制
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

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2017-10-21 06:26:23

不允许更改实体的主键-如果真的必须这样做,您应该删除并重新创建它。

参考(一个较老的问题):JPA Hibernate - changing the primary key of an persisted object

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46857976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档