hibernate中的java双向Map

piztneat  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(373)

我有两张表,报价单和报价单。他们之间有一对多的关系。我的要求是当我这样做 quotation.setQuotationItem(set) ,应删除Map到报价单的旧quotationitem,并将集合中的quotationitemsMap到报价单。
/我的报价pojo就像*/

public class Quotation {
    int quotationId;
    String code;
    String clientName;
    Set<QuotationItem> quotationItem=new HashSet<QuotationItem>();
    //getter and setter
    }

/报价项目pojo*/

public class QuotationItem extends Quotation{
    int id;
    Quotation quotation;
    String itemName;
    int rate;
    int qty;
    //getter and setter
    }

/报价.hbm******/

<hibernate-mapping>
    <class name="com.paramatrix.pojo.Quotation" table="quotation" >
    <id name="quotationId" type="int" column="quotation_id">
    <generator class="native" />
    </id>
    <property name="code" column="code" type="string" />
    <property name="clientName" column="client_name" type="string" />
    <set name="quotationItem" table="quotation_item" cascade="save-update" inverse="true">
    <key>
    <column name="quotation_id" not-null="true" />
    </key>
    <one-to-many class="com.paramatrix.pojo.QuotationItem" />
    </set>
    </class>
    </hibernate-mapping>

/报价项目.hbm****/

<class name="com.paramatrix.pojo.QuotationItem" table="quotation_item" dynamic-insert="true">
    <id name="id" type="int" column="id">
    <generator class="native" />
    </id>
    <many-to-one name="quotation" class="com.paramatrix.pojo.Quotation" >
    <column name="quotation_id" not-null="true" />
    </many-to-one>
    <property name="itemName" column="item_name" type="string" />
    <property name="rate" column="rate" type="int" />
    <property name="qty" column="qty" type="int" />
    </class>

我的表格结构像报价单 quotation_id code client_name 报价单项目的我的表结构 id quotation_id item_name rate qty

ozxc1zmp

ozxc1zmp1#

尝试修改集合而不是替换它。即。:

quotation.getQuotationItem().clear();
    quotation.getQuotationItem().addAll(newSet);

这是由于某种冬眠的怪异。
另外,还有一个关于命名的小问题。你的 Quotation.quotationItem 应命名为 quotationItems 因为它包含多个 QuotationItem .

相关问题