我正在尝试通过我的REST-api将新的Employee实体添加到数据库中。 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);
}
帮助将不胜感激!
答案 0 :(得分:3)
要设置双向多对一关联,您需要执行此操作:
在源/拥有实体(具有外键的实体)上,您当然使用@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
1}}。
现在,如果您尝试使用现有城市保存新员工,很明显您会收到您正在获得的错误,因为你试图保存一个新员工Hibernate将尝试保存现有的城市,但你不能用相同的PK两次保存相同的记录这就是Hibernate抱怨的原因:
Duplicate entry '1000' for key 'PRIMARY'
如果您想要在具有不同主键的所有相同城市记录之后,在保存实体之前,请将城市PK设置为null。
或者,就像我一样将级联移动到城市实体。 就像那样,每当你试图挽救一名员工时,这座城市也不会坚持下去。
或者只是将级联类型从all更改为除persist之外的每个级联类型。