I just started playing around with Hibernate and was running into problems trying to delete objects. My mapping file snippets & the code that does the delete is pasted below. Any help is really appreciated...
QualifierType.hbm.xml
=================
<?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="com.company.obj.QualifierType" table="COMPANY_QUALIFIER_TYPE">
<id name="id" type="int" column="TYPE_ID">
<meta attribute="scope-set">public</meta>
<generator class="native"/>
</id>
<property name="qualifierType" type="string"/>
<set name="qualValues" inverse="true" cascade="all-delete-orphan">
<key column="TYPE_ID"/>
<one-to-many class="com.company.obj.QualifierValues"/>
</set>
</class>
<query name="com.company.obj.allQualifierTypes">
<![CDATA[
from com.company.obj.QualifierType as qualTypes
]]>
</query>
<query name="com.company.obj.qualTypeByName">
<![CDATA[
from com.company.obj.QualifierType as qualTypes where upper(qualTypes.qualifierType) = upper(:qualtypename)
]]>
</query>
</hibernate-mapping>
QualifierValues.hbm.xml
==================
<?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="com.company.obj.QualifierValues" table="COMPANY_QUALIFIER_VALUES">
<id name="id" type="int" column="VALUE_ID">
<meta attribute="scope-set">public</meta>
<generator class="native"/>
</id>
<property name="qualValue" type="string"/>
<many-to-one
name="qualifierTypes"
column="TYPE_ID"
class="com.company.obj.QualifierType"
not-null="true"/>
</class>
<query name="com.company.obj.matchingQValues">
<![CDATA[
from com.company.obj.QualifierValues as qValuesObjs where qValuesObjs.id = :vIds
]]>
</query>
</hibernate-mapping>
As you would notice i have "type ---- (0..*)values" relationship. When i delete a type all the values go away as expected. When i try deleting the values (code pasted below) i keep getting exceptions...
net.sf.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations)
The code i have is pasted below:
public static void updateQualifierValues(
String arg_qualvalueids) throws Exception {
Configuration config = new Configuration();
config.addClass(QualifierType.class);
config.addClass(QualifierValues.class);
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.delete("select qValuesObjs from com.company.obj.QualifierValues as qValuesObjs");
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
sessionFactory.close();
}
My question is how should the code be written to help me delete all values. Part (b) is if i pass in a list of id's 21,22 as strings i want to issue a sql that looks like
DELETE FROM COMPANY_QUALIFIER_VALUES WHERE VALUE_ID IN (21,22)? How does the HSQL query look like. I am very new to this stuff so any help is really appreciated