-->
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: How can I use DB cascade delete in Hibernate?
PostPosted: Thu Jul 03, 2008 4:59 pm 
Newbie

Joined: Tue Apr 01, 2008 2:52 pm
Posts: 6
I have a hierarchy of classes with a single table per hierarchy but with joined secondary tables. Legacy DB schema has tables with cascade on delete. Therefore, to delete a record for a child it was enough to delete the associated record for the parent.
My question is how to tell Hibernate to use this feature?

To illustrate it, I created an example of classes and tables:

This is a parent class
Code:
[color=blue]@javax.persistence.Entity(name="Person")

@javax.persistence.Table(name="person")
@javax.persistence.DiscriminatorColumn(name="type")
@javax.persistence.Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class Person
{
    int id;
    String type;

    @javax.persistence.Id
    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    @javax.persistence.Basic
    public String getType()
    {
        return type;
    }

    public void setType(String type)
    {
        this.type = type;
    }

}[/color]

And this is a child
Code:
[color=blue]@Entity(name = "Customer")
@SecondaryTables(@SecondaryTable(name = "customer", pkJoinColumns = {
        @PrimaryKeyJoinColumn(name = "id")}))
@DiscriminatorValue(value = "customer")
public class Customer extends Person
{
    String account;

    @javax.persistence.Basic
    @Column(table = "customer")
    public String getAccount()
    {
        return account;
    }

    public void setAccount(String account)
    {
        this.account = account;
    }

} [/color]

DB Schema looks like:
Code:
[color=blue] CREATE TABLE `person` (
`id` int(11) NOT NULL,
`type` varchar(30) default NULL,

PRIMARY KEY  (`id`)
) ENGINE=InnoDB;

CREATE TABLE `customer` (
  `id` int(11) NOT NULL,
  `account` varchar(30) default NULL,
  PRIMARY KEY  (`id`),
  FOREIGN KEY (`id`) REFERENCES `person` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB; [/color]


If I issue sql command to DB:
Code:
[color=blue]delete from person where id=1;[/color]

it will delete records from person and from customer tables. I would expect Hibernate to do the same. However, if I execute HQL query through Hibernate:
Code:
[color=blue]delete Person where id=1[/color]

it will generate something like this:
Code:
[color=blue]create temporary table if not exists HT_person (id integer not null);
insert into HT_person select p.id as id from person p left outer join customer p1_ on p.id=c.id where type='customer' and (p.id=1)
delete from customer where (id) IN (select id from HT_person)
delete from person where (id) IN (select id from HT_person)
drop table HT_person[/color]


On large tables with many children it becomes a problem. So one more time, is it possible to teach Hibernate to use DB cascade delete?


Thank you,
Yevgeny


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 4:08 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
First of all: have a look into the documentation for the Hibernate annotations!

Here is an example to to use cascade delete on db level:
Code:
@OneToMany(mappedBy = "singleParty", cascade = { CascadeType.ALL }, fetch=FetchType.LAZY)   @org.hibernate.annotations.OnDelete(action=OnDeleteAction.CASCADE)
private Set<SingleFinance> finances = new HashSet<SingleFinance>();

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 1:43 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Quote:
Hibernate annotations!


And similarly, with JPA annotations.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 07, 2008 12:49 pm 
Newbie

Joined: Tue Apr 01, 2008 2:52 pm
Posts: 6
Probably, I was not clear enough. In my case, there is no any relation of type one-to-many to which I can apply the cascade annotation.
There is only inheritance. One class is derived from another. The derived class is mapped to two tables: the common one and the secondary one.
Naturally, the base class shall not have any references to the derived one.

I want to delete an instance of the derived class. If I used direct JDBC, it would be enough to delete a row from the common table. Cascading will do the rest.
However, Hibernate does an explicit deletion from both classes which is very inefficient.
My question is how to inform Hibernate about DB cascading capability not in case of object relationship but in case of inheritance.

Thanks,
Yevgeny


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.