-->
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: Database Level Cascade Delete
PostPosted: Wed Jan 24, 2007 12:03 pm 
Newbie

Joined: Thu Oct 21, 2004 4:22 am
Posts: 3
Hello,

I'm testing JPA with Hibernate, and can't annotate classes to produce foreign keys with delete cascade in SQL Server, I think it is possible, like is possible with hibernate core. Which could be the problem?

With this code I get a foreign key but delete cascade is not constrained
at database level.

Parent Class: PAIS
Child Class: PROVINCIA

Entity
@org.hibernate.annotations.Entity (dynamicInsert = true, dynamicUpdate = true)
@Table(name = "PAISES")
public class Pais implements Serializable{
@Id @GeneratedValue(generator = "hibernate-uuid")
@Column(name = "PAIS_ID")
private String id;
@Basic(optional = false)
@Column(name = "DESCRIPCION" , nullable = false)
private String descripcion;

@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE , CascadeType.REMOVE},
mappedBy = "pais")
@org.hibernate.annotations.Cascade(
value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN
)
private Set<Provincia> provincias = new HashSet<Provincia>();
public void setProvincias(Set<Provincia> provincias) {
this.provincias = provincias;
}
public Set<Provincia> getProvincias() {
return provincias;
}
public void addProvincia(Provincia provincia) {
provincia.setPais(this);
provincias.add(provincia);
}

public Pais() {}

.......................

@Entity
@org.hibernate.annotations.Entity (dynamicInsert = true, dynamicUpdate = true)
@Table(name = "PROVINCIAS")
@org.hibernate.annotations.Table(appliesTo="PROVINCIAS",
indexes = { @Index(name="IDX_PROVINCIAS_1", columnNames = { "PROVINCIA_ID", "DESCRIPCION" } ) } )
public class Provincia implements Serializable{
@Id @GeneratedValue(generator = "hibernate-uuid")
@Column(name = "PROVINCIA_ID", length = 32)
private String id;
@Basic(optional = false)
@Column(name = "DESCRIPCION" , nullable = false)
@org.hibernate.annotations.Index(name = "IDX_DESCRIPCION")
private String descripcion;

@ManyToOne
@OnDelete(action=OnDeleteAction.CASCADE)
@JoinColumn(name = "PAIS_ID", nullable = false)
@org.hibernate.annotations.ForeignKey(name = "FK_PAIS_ID")
private Pais pais;

public Provincia() {}

.......................


Thank's in advance


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 24, 2007 5:14 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
cah you post the working hbm mapping?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 25, 2007 9:10 am 
Newbie

Joined: Thu Oct 21, 2004 4:22 am
Posts: 3
This is an example of other projects and It'sworking ok:

With this files I get a Foreign Key with ON DELETE CASCADE in the table Localidades.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.drago.scr.model.bo">
<class name="Localidad" table="LOCALIDAD" optimistic-lock="none"
dynamic-insert="true" dynamic-update="true">
<id name="localidadId" type="string" unsaved-value="null">
<column name="localidad_id" length="32"/>
<generator class="uuid"/>
</id>
<property name="descripcion" type="string">
<column name="descripcion" not-null="true" length="80" unique-key="UI1_LOCALIDAD"/>
</property>
<many-to-one name="provincia" not-null="true" unique-key="UI1_LOCALIDAD"
class="Provincia" foreign-key="FkLocalidadProvincia"
column="provincia_id"/>
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.drago.scr.model.bo">
<class name="Provincia" table="PROVINCIA" optimistic-lock="none"
dynamic-insert="true" dynamic-update="true">
<id name="provinciaId" type="string" unsaved-value="null">
<column name="provincia_id" length="32"/>
<generator class="uuid"/>
</id>
<natural-id mutable="true">
<property name="provinciaCode" type="string">
<column name="provincia_code" length="20"/>
</property>
</natural-id>
<property name="descripcion" type="string">
<column name="descripcion" not-null="true" length="80"/>
</property>

<set name="localidades" inverse="true" cascade="all-delete-orphan">
<key column="provincia_id" on-delete="cascade"/>
<one-to-many class="Localidad"/>
</set>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 29, 2007 4:08 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If you define @OnDelete(action=OnDeleteAction.CASCADE) at the collection level (like in hbm) does it work?

_________________
Emmanuel


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.