0

There are certain things I do not understand about a small hibernate application for which the mapping xml is shown below.

    <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>

There are 2 pojos namely Ghazal and Singer. I understand the mapping of Singer class, but I do not understand that of the Ghazal class.

When I run the following program :

    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();

the data gets automatically stored in the three tables namely ghazal, linked, singer. How does the data get stored in the singer table while I committed only the object of ghazal class ?

And what does the tag :

<many-to-many...> 

do ?

Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328

1 Answers1

0

When instance of Ghazal is persisted via

sess.save(ghazal);

operation is cascaded to the instances of Singer that are in map referenced from Ghazal. Save (among other operations) is cascaded because attribute cascade is set to 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>

In this case many-to-many means that single instance of Singer can be in relation to many instances of Ghazal and single instance of Ghazal can be in relation to many instances of Singer. Collection mappings are documented for example in Hibernate reference documentation.

Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
  • _"..operation is cascaded to the instances of Singer that are in map referenced from Ghazal. Save (among other operations) is cascaded because attribute cascade is set to all.."_ can you please make it more clear ? I just didn't understand **'how'** is this happening ? Please explain the `cascade` attribute and what does actually the attribute `class` inside the `many-to-many` tag do ? – Suhail Gupta Jun 13 '13 at 12:06