I know this question has already been asked several times. Unfortunately, I have worked for 2 days to fix this issue :
Detached entity passed to persist
Here is my entities (Department and Area) :
@Audited
@Entity
public class Area implements Serializable {
/**
*
*/
private static final long serialVersionUID = -5555410199789696612L;
private Long id;
private Integer version;
private String name;
private String description;
private List<SubArea> subAreas=new ArrayList<SubArea>();
private Department department;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Version
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Machine [name=" + name + "]";
}
@NotAudited
@OneToMany(mappedBy="area",cascade=CascadeType.ALL)
public List<SubArea> getSubAreas() {
return subAreas;
}
public void setSubAreas(List<SubArea> subAreas) {
this.subAreas = subAreas;
}
/**
* @return the department
*/
@ManyToOne(cascade=CascadeType.PERSIST)
public Department getDepartment() {
return department;
}
/**
* @param department the department to set
*/
public void setDepartment(Department department) {
this.department = department;
}
@Transient
public SubArea addSubArea(SubArea subArea){
this.getSubAreas().add(subArea);
subArea.setArea(this);
return subArea;
}
AND
@Entity
@Audited
public class Department implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
// seam-gen attributes (you should probably edit these)
private Long id;
private Integer version;
private String name;
private List<Area> areas = new ArrayList<Area>();
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Version
public Integer getVersion() {
return version;
}
@SuppressWarnings("unused")
private void setVersion(Integer version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* @return the areas
*/
@NotAudited
@OneToMany(mappedBy = "department")
public List<Area> getAreas() {
return areas;
}
/**
* @param areas the areas to set
*/
public void setAreas(List<Area> areas) {
this.areas = areas;
}
@Transient
public Area addArea(Area area){
this.getAreas().add(area);
area.setDepartment(this);
return area;
}
Currently, I'm trying to save a new Area recently created into a Department field. By this way, I pass a department like argument and I'm able to recover it by my Repository method.
public String save(){
String redirection="";
try{
Department department;
department=departmentRepository.findBy(this.department.getId());
log.info("ID DEPARTMENT : "+department.getId());
area.setName(name);
log.info("NAME AREA : "+area.getName());
area.setDescription(description);
log.info("DESCRIPTION AREA : "+ area.getDescription());
area.setDepartment(department);
log.info("DEPARTMENT AREA : "+ area.getDepartment().getName());
department = departmentRepository.save(department);
log.info("DEPARTMENT SAVED");
areaRepository.save(area); //ERROR
log.info("ID AREA : "+area.getId());
jsfMessages.addInfo().operationCreationSucceeded();
redirection=AREAREDIRECT;
}
catch(Exception e){
jsfMessages.addInfo().operationCreationFailed();
e.printStackTrace();
}
return redirection;
}
The Area ID is AutoGenerated by an IDENTITY strategy. The Area's department is set, its name and description also. the save
method comes from the framework Apache DeltaSpike.
Please what's wrong with my code ? Is it an ID issue ? I have still lost a lot of time to fix the issue.
Thanks in advance for understanding.