【问题标题】:Java Persistence EntityNotFound Exception on an Object with Embedded Compound PK具有嵌入式复合 PK 的对象上的 Java 持久性 EntityNotFound 异常
【发布时间】:2018-09-21 13:23:19
【问题描述】:

我在 Java 持久性方面遇到了一些问题。我已经搜索了一些答案,发现一个问题是可能为 null 的字段未设置为 nullable = true。但是,这似乎对我不起作用。

我会更改一些名称,但请尽量解释清楚。

这是一个例外: EJB 异常:;嵌套异常是:javax.persistence.EntityNotFoundException:无法找到 ID 为 PersistedObjectPK@ec2736 的 PersistedObject;嵌套异常是:javax.persistence.EntityNotFoundException: Unable to find PersistedObject with id PersistedObjectPK@ec2736

PersistedObject 本身有一个主键,它被定义为一个具有复合主键的嵌入对象。

@Entity
@Table(name = "DB_TABLE_NAME", schema = "SCHEMA")
public class PersistedObject implements Serializable {

    private static final long serialVersionUID = -5823302417440143578L;

    @EmbeddedId
    private PersistedObjectPK id;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_POTATO", insertable = false, updatable = false)
    private AnotherPersistedObject anotherPersistedObject;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({
            @JoinColumn(name = "ID_CABBAGE", referencedColumnName = "ID_POTATO", insertable = false, updatable = false),
            @JoinColumn(name = "CT_CARROT", referencedColumnName = "CT_LETTUCE", insertable = false, updatable = false) })
    private OtherPersistedObject otherPersistedObject;

PersistedObjectPK 类如下:

@Embeddable
public class PersistedObjectPK implements Serializable {

    private static final long serialVersionUID = 4118584598667509460L;

    @Column(name = "ID_POTATO")
    private long idLaudo;

    @Column(name = "ID_CABBAGE")
    private long idTermoApreensao;

    @Column(name = "CT_CARROT")
    private long ctProduto;

我已经调试过了,查询直接在数据库中运行良好。 但我找不到导致此错误的原因。 该错误发生在 Merge 操作期间,系统请求保存更改后。

【问题讨论】:

    标签: java hibernate jakarta-ee persistence


    【解决方案1】:

    我找到了问题。

    在保存到数据库操作期间。

            AnotherPersistedObject term;
                //Previously, it did not do thi, it tried saving with the list, then clean and re-insert it.
                        List<PersistedObject > persistedObjects = term.getList();
                        term.setList(null);
                //After this change, it is now working as excpeted
                        this.getGenericBO().save(AnotherPersistedObject.class, term, term.getId());
                        term.setList(persistedObjects);
                        deleteList(term);
                        saveList(term);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-26
      • 1970-01-01
      相关资源
      最近更新 更多