0

关于下面显示的小型休眠应用程序,有些事情我不明白mapping xml

    <class name="pojo.Ghazal" table="ghazal">
        <id name="s_no">
            <generator class="increment" />
        </id>
        <property name="poem" />
        <property name="poet" />
        <map name="map" table="linked" cascade="all">
            <key column="s_no" />
            <index column="key_" type="string" />
            <many-to-many column="val_" class="pojo.Singer" />
        </map>
    </class>

    <class name="pojo.Singer" table="singer">
        <id name="s_no">
            <generator class="increment" />
        </id>
        <property name="singer_name" />
        <property name="country" />
    </class>

有 2 个,pojosGhazalSinger。我了解Singer类的映射,但我不了解类的映射Ghazal

当我运行以下程序时:

    Configuration config = new Configuration().configure();
    SessionFactory sessFact = config.buildSessionFactory();
    Session sess = sessFact.openSession();
    Transaction trans = sess.beginTransaction();
    Ghazal ghazal = new Ghazal();

    Singer singer = new Singer();
    singer.setCountry("Pakistan");
    singer.setSinger_name("Mehdi Hasan");
    Singer singer_1 = new Singer();
    singer_1.setCountry("India");
    singer_1.setSinger_name("Jagjit Singh");

    HashMap map = new HashMap();
    map.put("key_1", singer);
    map.put("key_2", singer_1);

    ghazal.setPoem("Woh Jo Hum Mein Tum Mein Qarar Tha");
    ghazal.setPoet("Momin Khan Momin");
    ghazal.setMap(map);

    sess.save(ghazal);
    trans.commit();

数据自动存储在三个表中ghazal, linked, singersinger当我只提交类的对象时,数据如何存储在表中ghazal

标签是什么:

<many-to-many...> 

吗?

4

1 回答 1

0

当实例Ghazal通过

sess.save(ghazal);

操作级联到映射中引用的 Singer 实例Ghazal。保存(在其他操作中)是级联的,因为属性级联设置为all.

<map name="map" table="linked" cascade="all">
    <key column="s_no" />
    <index column="key_" type="string" />
    <many-to-many column="val_" class="pojo.Singer" />
</map>

在这种情况下,多对多意味着单个实例Singer可以与 的多个实例相关,Ghazal而单个实例Ghazal可以与 的多个实例相关Singer。例如,在Hibernate 参考文档中记录了集合映射。

于 2013-06-13T08:24:28.933 回答