-->
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.  [ 1 post ] 
Author Message
 Post subject: Foreign key on delete cascade using link table (one-to-one)
PostPosted: Thu May 20, 2010 3:29 am 
Newbie

Joined: Thu May 20, 2010 2:22 am
Posts: 1
I need to have a link table that maps the ID of two classes that are persisted differently since they're used by two different programs. Class A is in the Java/JBoss/Javax/Hibernate domain while class B is used in the C++ domain and persisted by using DB access classes (class B is though realized on the Java side also in order for Hibernate to read its DB entries). Even though there shall exist a one-to-one relationship I do not want the removal of one instance of a class to cause a removal of it's mapped class (only an entry in the link table shall be removed, if any).

What I want to achieve is to make class A create a link table with two foreign keys that has the constraint "on delete cascade", in order for an entry in the link table to be removed if program A deletes an instance of class A, or if program B deletes an instance of class B. Without this constraint, program B cannot work correctly since it is unable to remove an entry in class B's database table. The link table is supposed to look like:
ID_A | ID_B
-----------
4 | 13
5 | 56
... | ...

I've got almost everything working except making Hibernate create the "on delete cascade"-constraints; from the hibernate log (edited for the general approach):
2010-05-19 11:37:49,199 DEBUG [org.hibernate.tool.hbm2ddl.SchemaUpdate] create table LINK_TABLE (A_id bigint not null, B_id bigint, primary key (A_id))

2010-05-19 11:37:49,855 DEBUG [org.hibernate.tool.hbm2ddl.SchemaUpdate] alter table LINK_TABLE add index FK265A97D4C167299D (B_id), add constraint FK265A97D4C167299D foreign key (B_id) references B (ID) [here I'd like Hibernate to also create ON DELETE CASCADE]

2010-05-19 11:37:49,887 DEBUG [org.hibernate.tool.hbm2ddl.SchemaUpdate] alter table LINK_TABLE add index FK265A97D4D17FA619 (A_id), add constraint FK265A97D4D17FA619 foreign key (A_id) references A (A_id) [here I'd like Hibernate to also create ON DELETE CASCADE]

Source code:
-- A.java
...
@Id
@GeneratedValue
@Column(name = "A_id")
private long m_id = -1;
...
@OneToOne(cascade=CascadeType.ALL)
@JoinTable(
name="LINK_TABLE",
joinColumns = @JoinColumn(name="A_id"),
inverseJoinColumns = @JoinColumn(name="B_id"))
private B m_b = null;
...
public B getB() {
return m_b;
}
...
public void setB(B b) {
this.m_b = b;
}
...

-- B.java
@Entity @Table(name = "B") // Program A shall only read from this table that is already present. Program B (C++) creates, reads and writes to this table.
public class B {

@Id @Column(name = "B_id")
@GeneratedValue()
private long m_id = -1;
...

I've tried to use the @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.ALL) annotation and @org.hibernate.annotations.OnDelete(action = org.hibernate.annotations.OnDeleteAction.CASCADE) without any success.

I'd be happy if you have any tip on how to make Hibernate create the "on delete cascade" constraint :)

Cheers!


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

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.