-->
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.  [ 7 posts ] 
Author Message
 Post subject: deleting from join table
PostPosted: Tue Jul 13, 2004 3:42 pm 
Beginner
Beginner

Joined: Mon Dec 08, 2003 12:15 am
Posts: 47
Hello:

I am using hibernate2.jar. What I want to do is to delete from a join table a single row. I have a many to many relationship between table ap_step and technology. The join table is ap_step_technology.

Here's the mapping for each object. For brevity I am including only the portion of interest:
ApStepVO.hbm.xml
<set
name="technologies"
table="ap_step_technology"
lazy="false"
inverse="false"
cascade="none"
sort="unsorted"
>

<key
column="ap_step_id"
/>

<many-to-many
class="com.securance.vo.TechnologyVO"
column="tech_id"
outer-join="auto"
/>

</set>
TechnologyVO.hbm.xml
<set
name="steps"
table="ap_step_technology"
lazy="false"
inverse="true"
cascade="none"
sort="unsorted"
>

<key
column="tech_id"
/>

<many-to-many
class="com.securance.vo.ApStepVO"
column="ap_step_id"
outer-join="auto"
/>

</set>

My code is as follows:

ApStepVO step = new ApStepVO();
step.setStepId(Integer.parseInt(deleteSteps[i]));
TechnologyVO tech = new TechnologyVO();
log.debug("The techId is " + techId);
tech.setTechnologyId(techId);
techs.add(techs);
step.setTechnologies(techs);
session.delete(step);
log.debug("Deleted step id: " + deleteSteps[i]);

and what ends up happening is that all rows with the given stepId are deleted from the join table. What I want is the row with the given stepId
and the given technologyId. The resulting sql is:

delete from ap_step_technology where ap_step_id=?
delete from ap_step where ap_step_id=?

when it what I want it to be is something like:

delete from ap_step where ap_step_id=? and tech_id=?

If possible can someone please point out where I am going wrong.

Thanks


Top
 Profile  
 
 Post subject: Re: deleting from join table
PostPosted: Thu Aug 19, 2004 2:36 pm 
Beginner
Beginner

Joined: Thu Aug 19, 2004 2:33 pm
Posts: 30
Location: CA, USA
Juan - I'm having the same problem. Did you ever find a solution?

Thanks,
Don

juan110470 wrote:
Hello:

I am using hibernate2.jar. What I want to do is to delete from a join table a single row. I have a many to many relationship between table ap_step and technology. The join table is ap_step_technology.

Here's the mapping for each object. For brevity I am including only the portion of interest:
ApStepVO.hbm.xml
<set
name="technologies"
table="ap_step_technology"
lazy="false"
inverse="false"
cascade="none"
sort="unsorted"
>

<key
column="ap_step_id"
/>

<many-to-many
class="com.securance.vo.TechnologyVO"
column="tech_id"
outer-join="auto"
/>

</set>
TechnologyVO.hbm.xml
<set
name="steps"
table="ap_step_technology"
lazy="false"
inverse="true"
cascade="none"
sort="unsorted"
>

<key
column="tech_id"
/>

<many-to-many
class="com.securance.vo.ApStepVO"
column="ap_step_id"
outer-join="auto"
/>

</set>

My code is as follows:

ApStepVO step = new ApStepVO();
step.setStepId(Integer.parseInt(deleteSteps[i]));
TechnologyVO tech = new TechnologyVO();
log.debug("The techId is " + techId);
tech.setTechnologyId(techId);
techs.add(techs);
step.setTechnologies(techs);
session.delete(step);
log.debug("Deleted step id: " + deleteSteps[i]);

and what ends up happening is that all rows with the given stepId are deleted from the join table. What I want is the row with the given stepId
and the given technologyId. The resulting sql is:

delete from ap_step_technology where ap_step_id=?
delete from ap_step where ap_step_id=?

when it what I want it to be is something like:

delete from ap_step where ap_step_id=? and tech_id=?

If possible can someone please point out where I am going wrong.

Thanks


Top
 Profile  
 
 Post subject: Re: deleting from join table
PostPosted: Thu Aug 19, 2004 2:49 pm 
Beginner
Beginner

Joined: Mon Dec 08, 2003 12:15 am
Posts: 47
My solution is below. It might not be the most elegant, but since no one else replied I went ahead and did it that way. The cool thing is that it can always be changed transparently since it is in a DAO way in the back-end :)

I'm still hoping to get a better solution.

public int saveTechnologyForUser(final SecuranceUserVO user, final TechnologyVO tech) {

HibernateTemplate template = getHibernateTemplate();
Object o = template.execute(
new HibernateCallback(){

public Object doInHibernate(Session session) throws HibernateException {

//session.lock(user, LockMode.NONE);
//session.lock(tech, LockMode.NONE);
//user.addTechnology(tech);
//session.save(user);
//session.update(user);
int rowsUpdated = 0;
Connection con = session.connection();
try{
PreparedStatement ps = con.prepareStatement("insert into user_technology values ( ?, ? )");
ps.setInt(1, user.getUserId().intValue());
ps.setInt(2, tech.getTechnologyId().intValue());
rowsUpdated = ps.executeUpdate();
log.info("There were " + rowsUpdated + " updated");
}
catch(Exception ex){
log.error("Caught Exception. The most likely cause is that the user is already associated with this technology", ex);
throw new RuntimeException(ex);
}
return new Integer(rowsUpdated);
}
}
);
return ((Integer)o).intValue();
}


Top
 Profile  
 
 Post subject: Re: deleting from join table
PostPosted: Thu Aug 19, 2004 2:54 pm 
Beginner
Beginner

Joined: Mon Dec 08, 2003 12:15 am
Posts: 47
Don I apologize, but my last post is not exactly the solution you needed.

But the idea is the same. Way back in your DAO, if you have to, just obtain a connection from the session and just use straight JDBC for things that Hibernate does not do that easily. I am sure some people hate this idea, but for us beginners it's the only thing we can do in the meatime.

Good luck


Top
 Profile  
 
 Post subject: Re: deleting from join table
PostPosted: Thu Aug 19, 2004 4:19 pm 
Beginner
Beginner

Joined: Thu Aug 19, 2004 2:33 pm
Posts: 30
Location: CA, USA
Juan,

Thanks for the reply. I appreciate your quick reply.

Don

juan110470 wrote:
Don I apologize, but my last post is not exactly the solution you needed.

But the idea is the same. Way back in your DAO, if you have to, just obtain a connection from the session and just use straight JDBC for things that Hibernate does not do that easily. I am sure some people hate this idea, but for us beginners it's the only thing we can do in the meatime.

Good luck


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 4:33 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
play with collection (remove) + understand cascade.
that will do what you need

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 5:24 pm 
Beginner
Beginner

Joined: Thu Aug 19, 2004 2:33 pm
Posts: 30
Location: CA, USA
Anthony,

Are you saying that this is something that has to be handled in my code or is it something Hibernate can do internally via the mapping file?

I'm trying to see if there is solution other than writing my code to check if a child belongs to another parent.

Don

anthony wrote:
play with collection (remove) + understand cascade.
that will do what you need


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