I am very new to Hibernate and trying to understand how it works. I am not sure of the semantics of many-to-one association. Any help would be appreciated.
I have used the exmaple Cat class in the reference doc and written a sampe app.
O-R Mapping is below
Code:
<class name="Cat" table="CATS" discriminator-value="C">
<id name="id" column="uid" type="long">
<generator class="hilo"/>
</id>
<discriminator column="subclass" type="character"/>
<property name="name"/>
<property name="birthdate" type="date"/>
<property name="color" not-null="true"/>
<property name="sex" not-null="true"/>
<property name="weight"/>
<many-to-one name="mate" column="mate_id" cascade="all"/>
<set name="kittens" cascade="all">
<key column="mother_id"/>
<one-to-many class="Cat"/>
</set>
</class>
The class definition is straight forward.
Code:
Cat a = new Cat(...);
Cat b = new Cat(...);
a.setMate(b);
b.setMate(a);
session.save(a);
session.save(b)
The above code generates the following SQL Statements
Hibernate: insert into CATS (name, birthdate, color, sex, weight, mate_id, subclass, uid) values (?, ?, ?, ?, ?, ?, 'C', ?)
Hibernate: insert into CATS (name, birthdate, color, sex, weight, mate_id, subclass, uid) values (?, ?, ?, ?, ?, ?, 'C', ?)
Hibernate: update CATS set name=?, birthdate=?, color=?, sex=?, weight=?, mate_id=? where uid=?
I understand that the
update attribute of properties is not set false and so the update is generated. But even if I set update to true why should it generate this SQL statement.
When should I set update to true or false?