Springboot JPA fügt einer neuen Einheit in einer Vielzahl eines Assoziation eine zugehörige Entität hinzuJava

Java-Forum
Anonymous
 Springboot JPA fügt einer neuen Einheit in einer Vielzahl eines Assoziation eine zugehörige Entität hinzu

Post by Anonymous »

Ich habe eine @manytoone -Beziehung zwischen einem Mitarbeiter und einer Abteilung. MitarbeiterePository mit der neuen Mitarbeitereinheit. Dies wirft mir jedoch einen "abgelösten Entität, der an persistiertes" -Fehler übergeben wurde. Das Problem tritt auf, wenn ich versuche, das Abteilungsunternehmen von einem bestehenden Mitarbeiter abzurufen und an einen neuen Mitarbeiter beizukommen und zu versuchen, die neue Mitarbeitereinheit zu sparen.@Entity
@Table(name = "employee")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class EmployeeEntity implements Serializable {

@Id
@SequenceGenerator(name = "employee_id_seq", sequenceName = "employee_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "employee_id_seq")
@Column(name = "id", updatable = false)
private Integer id;

@JdbcTypeCode(SqlTypes.Long)
@Column(name = "employee_id")
private Long employeeId;

@ManyToOne
@Cascade({CascadeType.ALL})
@JoinColumn(name = "department_id", nullable = false)
private DepartmentEntity departmentEntity;

public EmployeeEntity(Long employeeId, DepartmentEntity departmentEntity) {
this.employeeId = employeeId;
this.departmentEntity = departmentEntity;
}
}

@Entity
@Table(name = "department")
@Data
@NoArgsConstructor
public class DepartmentEntity implements Serializable {
@Id
@SequenceGenerator(name = "department_id_seq", sequenceName = "department_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "department_id_seq")
@Column(name = "id", updatable = false)
private Integer id;

@JdbcTypeCode(SqlTypes.Integer)
@Column(name = "num_employees", nullable = false)
private Integer numEmployees;

}

// code execution
@Transactional
public void addEmployee(employeeId, managerId) {
Employee manager = empRepository.findById(managerId);

Department empDept = manager.getDepartmentEntity()
empDept.numEmployees++;

Employee associate = new EmployeeEntity(employeeId, empDept);

empRepository.save(associate); // this throws the exception
}
< /code>
Irgendwelche Gedanken darüber, wie dies angesprochen werden kann? Vielen Dank im Voraus!
sg

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post