传递给分离的实体持久化ISSUE

时间:2017-07-12 13:36:33

标签: hibernate java-ee deltaspike

我知道这个问题已被问过好几次了。不幸的是,我已经工作了2天来解决这个问题:

Detached entity passed to persist 

这是我的实体(部门和区域):

@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;
}

@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;
}

目前,我正在尝试将最近创建的新区域保存到部门字段中。通过这种方式,我通过一个像论证这样的部门,我可以通过我的Repository方法恢复它。

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;
}

区域ID由IDENTITY策略自动生成。区域部门已设置,其名称和描述也已设置。 save方法来自Apache DeltaSpike框架。

请问我的代码有什么问题?这是ID问题吗?我仍然花了很多时间来解决这个问题。

提前感谢您的理解。

0 个答案:

没有答案