Hibernate version 2.1.3
MySQL database
Code:
public void Store(Component comp) throws HibernateException {
Session session = sessions.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.saveOrUpdate(comp);
session.flush();
tx.commit();
}
catch (HibernateException he) {
if (tx!=null) tx.rollback();
throw he;
}
finally {
session.close();
}
}
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="logic.Component"
table="COMPONENT">
<id
name="id"
column="ID"
type="java.lang.Long"
unsaved-value="null">
<generator class="foreign">
<param name="property">price</param>
</generator>
</id>
<discriminator
column="DISCRIMINATOR"
type="string"/>
<property
name="name"
column="NAME"
not-null="false"
unique="true"
type="java.lang.String"/>
<property
name="description"
column="DESCRIPTION"
not-null="false"
unique="false"
type="java.lang.String"/>
<property
name="photo"
column="PHOTO"
not-null="false"
unique="false"
type="blob"/>
<bag
name="children"
inverse="true"
lazy="false"
cascade="save-update">
<key column="PARENT"/>
<one-to-many class="logic.Component"/>
</bag>
<one-to-one name="price" class="logic.Currency"/>
<many-to-one
name="parent"
column="PARENT"
not-null="false"
cascade="save-update"
class="logic.Component"/>
<subclass
name="logic.CompositeComponent"
discriminator-value="composite"/>
<subclass
name="logic.Item"
discriminator-value="item"/>
<subclass
name="logic.StdComposite"
discriminator-value="stdcomposite"/>
<subclass
name="logic.ModelComposite"
discriminator-value="modelcomposite"/>
</class>
</hibernate-mapping>
No exceptions occured.