• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on deletes

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"grandpa"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Load QualifierType.
2. Get the qualVAlues (set) from the loaded QUalifierType.
3. Remove the qualValues that you want to delete from that set
4. Resave QualifierType with the set that contains only those QualValues that you want to retain.
 
Screaming fools! It's nothing more than a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic