0

この質問はすでに何度か聞かれていることを知っています。残念ながら、私はこの問題を修正するために 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の問題ですか?私はまだ問題を解決するために多くの時間を失っています.

ご理解いただきありがとうございます。

4

0 に答える 0