gpt4 book ai didi

java - CascadeType.ALL 不更新子元素

转载 作者:行者123 更新时间:2023-11-30 06:51:18 26 4
gpt4 key购买 nike

我正在尝试通过我的 REST-api 向数据库添加一个新的员工实体。 Employee-entity 包含一个 City 属性。这是一个 ManyToOne 关系。当我插入一个新的(不存在的)城市的新员工时,它工作正常。但是,当我在现有城市中添加新员工时,出现此错误。

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1000' for key 'PRIMARY'

我的 CascadeType 设置为 ALL。当我将其更改为 SAVE_UPDATE 时,当我插入一个具有现有城市的新员工时它工作正常,但当我插入一个具有新城市的新员工时它不起作用。

员工:

package be.pxl.backend.entity;

import java.util.Date;
import java.util.List;
import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.*;
import org.hibernate.annotations.CascadeType;

@Entity
@Table(name="Employees")
public class Employee {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int employee_id;
private String username;
private String password;
private String salt;
private String surName;
private String name;
private String street;
@Column(name="house_nr")
private String houseNr;
@ManyToOne
@Cascade (value={CascadeType.MERGE, CascadeType.PERSIST})
private City city;
private Date date_employment;
private String mobile_phone;
private String telephone_number;
private String email;
private String sex;
private Boolean status;
@OneToMany(mappedBy="employee")
private List<Login> logins;
@OneToMany(mappedBy = "employee")
private List<SensorUsage> usages ;

public List<Login> getLogins() {
return logins;
}
public Boolean getStatus() {
return status;
}
public int getEmployee_id() {
return employee_id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getSalt() {
return salt;
}
public String getSurName() {
return surName;
}
public String getName() {
return name;
}
public String getStreet() {
return street;
}
public String getHouseNr() {
return houseNr;
}
public City getCity() {
return city;
}
public Date getDate_employment() {
return date_employment;
}
public String getMobile_phone() {
return mobile_phone;
}
public String getTelephone_number() {
return telephone_number;
}
public String getEmail() {
return email;
}
public String getSex() {
return sex;
}

public void setStatus(boolean status) { this.status = status; }

}

城市:

package be.pxl.backend.entity;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name="Cities")
public class City {

@Id
private String postal_code;
private String city;
@OneToMany(mappedBy = "city")
private List<Destination> destinations;
@OneToMany(mappedBy = "city")
private List<Employee> employees;

public String getPostal_code() {
return postal_code;
}
public String getCity() {
return city;
}

}

这是我坚持的地方:

package be.pxl.backend.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import be.pxl.backend.entity.*;

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
@Query("from Employee e where e.username = :username")
Employee getEmployeeByUsername(@Param("username")String username);
}

帮助将不胜感激!

最佳答案

为了设置双向多对一关联,您需要这样做:

当然,在源/拥有实体(具有外键的实体)上,您使用 @ManyToOne 注释对其进行注释,并使用 @JoinColumn 对其进行注释注解。在您的情况下,这就是它在 Employee 实体上的样子:

@Entity
@Table(name="Employees")
public class Employee {

@ManyToOne
@JoinColumn(name = "your_fk_column")
private City city;
}

然后在您的 City 实体上执行此操作:

@Entity
@Table(name="Cities")
public class City {

@OneToMany(cascade = CascadeType.ALL, mappedBy = "city")
private List<Employee> employees;
}

CascadeType.ALL 将引用您的示例执行的操作之一是,当您尝试保留/保存员工时,它会对关联的城市执行相同的操作,因为 CascadeType.ALL.

现在,如果您尝试使用现有城市保存一名新员工,很明显您会得到您遇到的错误,因为当您尝试保存一名新员工时,Hibernate 会尝试保存现有的城市,但是你不能保存具有相同 PK 的相同记录两次这就是 Hibernate 提示的原因:

Duplicate entry '1000' for key 'PRIMARY'

如果你想在所有同一个城市记录不同的主键,就在保存实体之前,将城市PK设置为空。

或者,像我一样将级联移动到城市实体。就像那样,每当您尝试拯救一名员工时,城市也不会持久存在。

或者只是将级联类型从所有更改为除持久性之外的每个级联类型。

关于java - CascadeType.ALL 不更新子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40319984/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com