javax.persistence.EntityNotFoundException:无法找到带有id的com ...

时间:2016-09-09 08:08:12

标签: java hibernate jpa jpa-2.0 hibernate-mapping

我目前正致力于将项目从Hibernate XML映射迁移到JPA注释。我解决了最多,但我遇到了其中一个关系的问题。原始的XML映射是:

<set name="projectCopies">
    <key column="parent_id"/>
    <one-to-many class="com...Project"/>
</set>

我注释了这个集合:

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name="parent_id") 
private Set<Project> projectCopies=new HashSet<Project>();

当我查看数据库时似乎正在工作:

+--------------+--------------------------+-----------+ 
| HIBERNATE_ID | name                     | parent_id |
+--------------+--------------------------+-----------+ 
|            4 | Test_project             |      NULL | 
|            5 | Test_project_copy        |         4 |
+--------------+--------------------------+-----------+

问题是它只在我添加项目副本时才第一次起作用。删除副本并尝试添加新副本后,我收到以下错误:

  

javax.persistence.EntityNotFoundException:无法找到   com ...项目ID为

我试图以多种方式修复,但没有任何效果。我很感激任何想法。

一些代码:

public final Project getMyCopy() {
    if (!hasCopy()) {
        return this;
    }
    return copies.iterator().next();
}

protected final void clearCopies() {
    if (hasCopy()) {
        delete(getMyCopy());  <-- delete method only contains: JpaStore.PROJECT.delete(project.getId());
    }
    copies.clear();
    copyDate=0;
    JpaStore.PROJECT.merge(this);
}


public final void copy() {
    try {
        if (hasCopy()) {
            clearCopies();
        }
        final Project copy=this.clone();
        copy.name=makeCopyName(name);
        copy.stampDate=COPY_COPYDATE;
        setCopyDate();
        setMyCopy(copy);
        save();  <-- save method only contains: JpaStore.PROJECT.merge(this);

    } catch (final CloneNotSupportedException e) {
        throw new RuntimeException(""+e);
    }
}

0 个答案:

没有答案