-->
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.  [ 11 posts ] 
Author Message
 Post subject: cannot delete object(parent with children)
PostPosted: Sat Nov 14, 2009 2:11 pm 
Newbie

Joined: Sat Nov 14, 2009 2:07 pm
Posts: 10
transaction = session.beginTransaction();
XXX x = (XXX) session.get(XXX.class, id);
session.beginTransaction();
session.delete(x);
transaction.commit();

does not delete x ?

i have one to many relationships eg
<bag name="degrees" cascade="all,delete-orphan">
<key column="PARENT_ID" not-null="true"/>
<one-to-many class="candidate.Degree"/>
</bag>


what am i doing wrong?


Top
 Profile  
 
 Post subject: Re: cannot delete object(parent with children)
PostPosted: Sat Nov 14, 2009 2:44 pm 
Newbie

Joined: Sat Nov 14, 2009 12:16 pm
Posts: 7
It should be cascade="all-delete-orphan" not cascade="all,delete-orphan" ...I think thats the problem


Top
 Profile  
 
 Post subject: Re: cannot delete object(parent with children)
PostPosted: Sat Nov 14, 2009 2:50 pm 
Newbie

Joined: Sat Nov 14, 2009 2:07 pm
Posts: 10
thank you for reply

but nope the object is still there

i try to delete object then i redirect the servlet that is to delete the object to jsp page that displays all the records and it is still there


Top
 Profile  
 
 Post subject: Re: cannot delete object(parent with children)
PostPosted: Sat Nov 14, 2009 3:24 pm 
Newbie

Joined: Sat Nov 14, 2009 12:16 pm
Posts: 7
although i am not entirely clear... but I see you are starting the transaction twice.. Only once is fine...

session.beginTransaction();
XXX x = (XXX) session.get(XXX.class, id);
session.delete(x);
session.getTransaction().commit();

may be this will help..


Top
 Profile  
 
 Post subject: Re: cannot delete object(parent with children)
PostPosted: Sat Nov 14, 2009 4:12 pm 
Newbie

Joined: Sat Nov 14, 2009 2:07 pm
Posts: 10
ups let it there when i updated the code
but still it does not delete records


Top
 Profile  
 
 Post subject: Re: cannot delete object(parent with children)
PostPosted: Sat Nov 14, 2009 4:13 pm 
Newbie

Joined: Sat Nov 14, 2009 12:16 pm
Posts: 7
will it be possible for u to post the code.. and the hbm file..for you


Top
 Profile  
 
 Post subject: Re: cannot delete object(parent with children)
PostPosted: Sat Nov 14, 2009 4:40 pm 
Newbie

Joined: Sat Nov 14, 2009 2:07 pm
Posts: 10
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="candidate.Candidate" table="CANDIDATES">
<id name="ID" type="long" column="ID_CANDIDATE">
<generator class="increment"/>
</id>


<property name="lastName">
<column name="lastname"/>
</property>
<property name="firstName">
<column name="firstname"/>
</property>



<property name="civicStatus">
<column name="civicStatus"/>
</property>

<property name="dob">
<column name="dob"/>
</property>
<property name="phMob">
<column name="phMob"/>




<property name="emailPer">
<column name="emailper"/>
</property>
<property name="emailWork">
<column name="emailWork"/>
</property>




<bag name="degrees" cascade="all-delete-orphan">
<key column="PARENT_ID" not-null="true"/>
<one-to-many class="candidate.Degree"/>
</bag>




</class>
</hibernate-mapping>




and the code is

public void deleteCandidate(long id){
try {
transaction = session.beginTransaction();
Candidate candidate = (Candidate) session.get(Candidate.class, id);
if(candidate==null)
System.out.println("kandydat null");
session.delete(candidate);
transaction.commit();
}catch(HibernateException e){
transaction.rollback();
}

}

and in the servlet

for(int i=0; i<candidatesID.length;i++){
db.setSession(); // that is in the db class that manages the sessions
long candidateID = Long.parseLong(candidatesID[i].trim());
db.deleteCandidate(candidateID);
db.closeSession();
}


Top
 Profile  
 
 Post subject: Re: cannot delete object(parent with children)
PostPosted: Sat Nov 14, 2009 5:32 pm 
Newbie

Joined: Sat Nov 14, 2009 12:16 pm
Posts: 7
can u paste the mapping class too for candidate and degree


Top
 Profile  
 
 Post subject: Re: cannot delete object(parent with children)
PostPosted: Sat Nov 14, 2009 6:29 pm 
Newbie

Joined: Sat Nov 14, 2009 2:07 pm
Posts: 10
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="candidate.Degree" table="CANDIDATES_EDUCATION">
<id column="ID_EDUCATION" name="ID_EDUCATION" type="long" unsaved-value="0">
<generator class="increment"/>
</id>

<property name="level">
<column name="LEVEL"/>
</property>

<property name="year" type="short">
<column length="4" name="YEAR"/>
</property>

<property name="specialty">
<column name="SPECIALTY"/>
</property>

<property name="school">
<column name="SCHOOL"/>
</property>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: cannot delete object(parent with children)
PostPosted: Sat Nov 14, 2009 7:05 pm 
Newbie

Joined: Sat Nov 14, 2009 12:16 pm
Posts: 7
<bag name="degrees" cascade="all-delete-orphan">
<key column="PARENT_ID" not-null="true"/>
<one-to-many class="candidate.Degree"/>
</bag>

what key "PARENT_ID" REFERS TO??.. do u have a column of that name?? Change it to ID_CANDIDATE...the column name of the parent class Id


Top
 Profile  
 
 Post subject: Re: cannot delete object(parent with children)
PostPosted: Sun Nov 15, 2009 8:28 pm 
Newbie

Joined: Sat Nov 14, 2009 2:07 pm
Posts: 10
i have a column of that name and it is a foreign key to ID_CANDIDATE


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.