Hi,
I currently facing a problem creating a foreign key with ON DELETE CASCADE attribute.
Here are my entities :
- One main entity BoxWcsKlEntityBean, which is linked to an abstract entity AbstractBoxDdWcsKlEntityBean, this second one extends from another abstract mappedsuperclass AbstractBoxModuleWcsKlEntityBean. And after all, one class BoxWcsDdEntityBean which extends from AbstractBoxDdWcsKlEntityBean :
Code:
@Entity(name = BoxWcsKlEntityBean.ENTITY_NAME)
@Table(name = BoxWcsKlEntityBean.TABLE_NAME)
public class BoxWcsKlEntityBean {
...
@OneToOne(mappedBy = "box", cascade = { CascadeType.PERSIST, CascadeType.REMOVE }, fetch = FetchType.LAZY, optional = true)
public AbstractBoxDdWcsKlEntityBean getBoxDd() {
return boxDd;
}
...
}
@Entity(name = AbstractBoxDdWcsKlEntityBean.ENTITY_NAME)
@Table(name = AbstractBoxDdWcsKlEntityBean.TABLE_NAME)
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractBoxDdWcsKlEntityBean
extends AbstractBoxModuleWcsKlEntityBean {
...
}
@MappedSuperclass
public abstract class AbstractBoxModuleWcsKlEntityBean {
...
private BoxWcsKlEntityBean box;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_BOX", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
public BoxWcsKlEntityBean getBox() {
return box;
}
}
@Entity(name = BoxWcsDdEntityBean.ENTITY_NAME)
@Table(name = BoxWcsDdEntityBean.TABLE_NAME)
public class BoxWcsDdEntityBean
extends AbstractBoxDdWcsKlEntityBean {
...
}
In the MappedSuperclass, I have notified a @OnDelete tag in order to create a ON DELETE CASCADE contraint, so that when I delete a BoxWcsKlEntityBean, database will delete in cascade the BoxWcsDdEntityBean associated to it (I want database to do it, not hibernate !).
So as inheritance is TABLE_PER_CLASS, two tables are created : one for entity BoxWcsKlEntityBean and the other one for BoxWcsDdEntityBean. The problem is that foreign key is well created in table BoxWcsDdEntityBean, but not with the ON DELETE CASCADE attribute.....
I tried to override the getBox() method in BoxWcsDdEntityBean, like this :
Code:
@Override
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_BOX", nullable = false, insertable = false, updatable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
public BoxWcsKlEntityBean getBox() {
return super.getBox();
}
But it fails with a duplicate column name ID_BOX.
Any idea of what happens ?
Thanks in advance for your answer.
Steve