-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Delete question
PostPosted: Wed Aug 03, 2005 10:28 pm 
Newbie

Joined: Tue Jun 14, 2005 10:54 am
Posts: 4
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 04, 2005 4:50 am 
Beginner
Beginner

Joined: Wed Jun 08, 2005 10:01 am
Posts: 22
Location: Italy
Hallo !

I suggest you to read the manual.
In particular the capter "working with objects".

In case you want to delete a determined ID
you have to load your object:
Code:
Cat cat = (Cat) session.load(Cat.class, catID);


and then (read "Deleting persistent objects")

Code:
session.delete(cat);


If you want to erase a table, I'd write a native query.

Bye,
nic


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 04, 2005 5:53 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
Alternatively, you could use a bulk delete to delete all of your QualifierValues objects:
Code:
session.createQuery("delete from QualifierValues").executeUpdate();

This is only available with Hibernate 3. See the documentation.

Best regards
Sven


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 04, 2005 8:15 am 
Newbie

Joined: Tue Jun 14, 2005 10:54 am
Posts: 4
I have tried all these techniques. I keep getting the following exception
deleted object would be re-saved by cascade (remove deleted object from associations): 28, of class: com.company.obj.QualifierValues

If i remove the attribute cascade="all-delete-orphan" from the QualifierTypes to which this object has a 0..* relationship i am being able to delete but then i loose the ability to delete a whole association if i choose to delete the type.

I would like to have the following ability
a) If i delete type all the values associated with the type should be deleted
(what i am bein able to achieve using cascade="all-delete-orphan")

b) I should be able to delete individual values (this is not happening in my case)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.