孩子没有脱离父母

时间:2015-09-24 06:45:54

标签: java hibernate jpa spring-data-jpa

我的文档实体类是

    @Entity
    @Table(name="Document_Directory")
    public class DocumentDirectoryEntity{

    private int id;
    private String name;
    private DocumentDirectoryEntity parentDirectory;
    private List<DocumentDirectoryEntity> childDirectoryList;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    @Column(name="name", length=250, nullable= false)
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="parent_dir_id")
    public DocumentDirectoryEntity getParentDirectory() {
        return parentDirectory;
    }
    public void setParentDirectory(DocumentDirectoryEntity parentDirectory) {
        this.parentDirectory = parentDirectory;
    }

    @OneToMany(mappedBy="parentDirectory", cascade=CascadeType.ALL, fetch=FetchType.LAZY, orphanRemoval=true)
    public List<DocumentDirectoryEntity> getChildDirectoryList() {
        return childDirectoryList;
    }
    public void setChildDirectoryList(List<DocumentDirectoryEntity> childDirectoryList) {
        this.childDirectoryList = childDirectoryList;
    }
}

我想要删除文档,但在删除之前,我想要更改其子级的父级。但我无法做到。代码是: -

DocumentDirectoryEntity documentDirectoryEntity = documentDirectoryDao.findById(1);
DocumentDirectoryEntity documentDirectoryEntity1 = documentDirectoryDao.findById(2);
List<DocumentDirectoryEntity> childDirectoryList = documentDirectoryEntity.getChildDirectoryList();
DocumentDirectoryEntity childDirectory = childDirectoryList.get(0);
childDirectory.setParentDirectory(documentDirectoryEntity1);
documentDirectoryDao.update(childDirectory);
documentDirectoryDao.delete(documentDirectoryEntity);

当我删除对象documentDirectoryEntity时,也会删除childDirectory行。

0 个答案:

没有答案