-->
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.  [ 8 posts ] 
Author Message
 Post subject: Delete problem
PostPosted: Thu May 18, 2006 6:44 pm 
Newbie

Joined: Wed Apr 06, 2005 11:41 am
Posts: 13
I have a class A having id,name and list of some integers which are ordered.Class A is mapped to Table A and the list of value type in this case integers are mapped to table B.When I want to delete class A the user passes id of some object of A, the row for this id should be deleted as well as its associated children.But I dont want to use load and then delete,instead I want to know if I can delete using HQL query.I tried this but I ended up with the following error

org.springframework.dao.DataIntegrityViolationException: Hibernate operation: could not execute update query; SQL [delete from A where ID=?]; Integrity constraint violation FKCC295055F9F89D2D table: B in statement [delete from A where ID=?];

First of all,how would the mapping look like?
Is there a way out for the above problem?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 7:47 pm 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
this basically depends on your db design.

table B should have a foreign key to A - basically FKCC295055F9F89D2D
if you have mapped A-->B as one-to-many

if you are sure that A-->B is a "contained" relationship i.e. if A is deleted you want to delete B add on-delete="cascade" on the one-to-many relation OR just alter the constraint in the DB directly.

any way you do it, now if you delete A with HQL all the B's will go away without having to load them into memory


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 19, 2006 6:57 am 
Newbie

Joined: Wed Apr 06, 2005 11:41 am
Posts: 13
I have the following class with hibernate annotation

@Entity
@Table(name = "A")
@Proxy(lazy = false)
public class A {
private Integer id;
private List<Integer> masterIds;

@Id @GeneratedValue(generator = "uuid-generator")
@Column(name = "A_ID", nullable = false)
public Integer getId() {
return id;
}

@OneToMany(cascade=CascadeType.ALL)
@JoinTable(
name = "B",
joinColumns = @JoinColumn(name="A_ID"),
inverseJoinColumns = @JoinColumn(name="B_ID")
)
@IndexColumn(name="INDEX")
public List<Integer> getMasterIds() {
return null; //assume we return a ArrayList here
}
}

If I try to fire following HQL query to delete using the id I get the
"delete from "+A.class.getName()+ " a " +" where a.id = "+id;

I get the exception mentioned in my first post.
Can you tell me what I am doing wrong in the above mapping


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 19, 2006 10:55 am 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
same as what i said in my post. i'm not using annotations yet so it might looks like this:

Code:
@OneToMany(cascade=CascadeType.ALL)
@OnDelete(action=OnDeleteAction.CASCADE)
@JoinTable(name = "B", joinColumns = @JoinColumn(name="A_ID"), inverseJoinColumns = @JoinColumn(name="B_ID"))
@IndexColumn(name="INDEX")
public List<Integer> getMasterIds()
{
    return null; //assume we return a ArrayList here
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 19, 2006 11:32 am 
Newbie

Joined: Wed Apr 06, 2005 11:41 am
Posts: 13
I tried the same also, with @OnDelete(action=OnDeleteAction.CASCADE)

but it didnt work gave me the DataIIntegrityViolationException.

Did u check the query that I use, I thought with the above mapping/annotations hibernate would take care of deletion of children, if i fire that query.But :(

If I check the sql queries before delete an update query is fired I dont understand why...could it be cos of that?


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 20, 2006 1:44 am 
Newbie

Joined: Wed Apr 06, 2005 11:41 am
Posts: 13
I read somewhere that HQL delete cascade is not possible and the hibernate guys are working on it...Is this true? So what I am trying to achieve isnt possible for now?


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 20, 2006 2:46 am 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
the cascade deletes work fine if they are defined correctly at the db level. i use them qutie a bit myself. instead of xdoclet i directly add cascade constraints in the database

based on your db just add a constraint that looks something like

Code:
alter table B add constraint FK_B_A fk_a reference a (id)


check the manual for the correct syntax. this is how you can efficiently do bulk deletes


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 20, 2006 4:54 am 
Newbie

Joined: Wed Apr 06, 2005 11:41 am
Posts: 13
Can you send me a sample program if you have one...


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