1

リストが休止状態でどのように管理されるかを理解するといういくつかの課題に直面しています。

私は次の投稿を見てきました: リストからアイテムを削除する休止状態は持続しませんが、それは役に立ちませんでした。

したがって、ここに親があります:

public class Material extends MappedModel implements Serializable
{
    /**
     * List of material attributes associated with the given material
     */
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    @JoinColumn(name = "material_id", nullable = false)
    @org.hibernate.annotations.IndexColumn(name = "seq_number", base = 0)
    private List<MaterialAttribute> materialAttributes = new ArrayList<MaterialAttribute>();

ここに子供がいます

public class MaterialAttribute extends MappedModel 実装 Serializable

{
    /**
     * identifies the material that these attributes are associated with
     */
    @ManyToOne
    @JoinColumn(name = "material_id", nullable=false, updatable=false, insertable=false)
    private Material material;

したがって、私の Service クラスでは、次のことを行っています。

public void save(MaterialCommand pCmd)
{
Material material = new Material();
if(null != pCmd.getMaterialId())
{
    material = this.loadMaterial(pCmd.getMaterialId());
}

material.setName(pCmd.getName());
material.getMaterialAttributes().clear();

    List<MaterialAttribute> attribs = new ArrayList<MaterialAttribute>();
    if(CollectionUtils.isNotEmpty(pCmd.getAttribs()))
    {
        Iterator<MaterialAttributeCommand> iter = pCmd.getAttribs().iterator();
        while(iter.hasNext())
        {
MaterialAttributeCommand attribCmd = (MaterialAttributeCommand) iter.next();
            if (StringUtils.isNotBlank(attribCmd.getDisplayName()))
{
        MaterialAttribute attrib = new MaterialAttribute();
        attrib.setDisplayName(attribCmd.getDisplayName());
        attrib.setValidationType(null);
        attribs.add(attrib);
}
        }
    }

    material.setMaterialAttributes(attribs);
    this.getMaterialDao().update(material);
}

したがって、上記のセットアップでは、最初にデータベースにすべてが正しく作成されます。2 回目は、コレクション内の最初のアイテム セットが削除され、新しいアイテムのみがデータベースに追加されると予想していました。それは起こりませんでした。子の元のアイテムは新しいアイテムとともにそこにあり、seq 番号は再び 0 から始まります。

また、次のエラーが表示されます

org.hibernate.HibernateException: cascade="all-delete-orphan" を持つコレクションは、所有エンティティ インスタンスによって参照されなくなりました:

何が欠けていますか。

4

1 に答える 1

4

の実装でDELETE_ORPHANは、コレクションの操作にいくつかの制限が適用されます。特に、そのコレクションをコレクションの別のインスタンスに置き換えるべきではありません。代わりに、既存のコレクションに新しいアイテムを追加します。

material.getMaterialAttributes().clear();
... 
material.getMaterialAttributes().addAll(attribs); 
于 2011-03-24T19:50:38.553 に答える