簡體   English   中英

Hibernate一對多HashMap沒有更新孩子

[英]Hibernate One-to-many HashMap not updating on child

我有以下父對象,它映射到我的數據庫中的表:

public Parent {
    private Long id;
    private String mid;
    private Integer days;
    private BigDecimal fee;
    private DateTime createdDate = new DateTime();
    private DateTime lastModifiedDate;

    private Map<String, Child> children;
}

使用以下.hbm.xml:

    <hibernate-mapping default-access="field">
    <class name="Parent" table="parent_table">
        <id column="id" length="50" name="id" unsaved-value="null">
            <generator class="increment"/>
        </id>
        <property length="50" name="mid"/>
        <property name="days"/>
        <property name="fee"/>
        <property name="createdDate" type="(...)PersistentDateTime"/>
        <property name="lastModifiedDate" type="(...)PersistentDateTime"/>
        <map cascade="all-delete-orphan" inverse="true" name="children" >
            <key column="parentId" />
            <map-key column="country" type="string" />
            <one-to-many class="Child" />
        </map>
    </class>
    </hibernate-mapping>

子對象如下:

public class Child implements Serializable {
    private Long parentId;
    private String country;
    private String cu;
}

使用以下.hbm.xml:

<hibernate-mapping default-access="field">
<class name="Child" table="child_table">
    <composite-id>
        <key-property name="parentId"/>
        <key-property name="country"/>
        <key-property name="cu"/>
    </composite-id>
</class>
</hibernate-mapping>

通過以下方式從我的數據庫中獲取父對象后:

getSession().createCriteria(Parent.class)
                            .add(Restrictions.eq("mid", mid))
                            .uniqueResult();

在子映射中對Child.cu進行一些更改后,我在Parent對象上調用saveOrUpdate。 執行此操作后,所有似乎都保存/更新正常但在檢查數據庫中的child_table時,這些更改尚未保存/更新。

我相信這與Parent類中的映射映射有關,但似乎無法弄明白。 任何幫助,將不勝感激。

提前致謝。

如果我理解正確,您正在修改一個字段,該字段是您實體的主鍵的一部分。 這是非法的:ID應該是不可變的。

我的建議是遵循良好實踐:使用非復合,純技術,自動生成的主鍵。 一切都會變得更簡單(也更快)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM