Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Cascade delete not desired, on a ManyToMany relation
Cascade delete not desired, on a ManyToMany relation [message #676245] Fri, 03 June 2011 11:20 Go to next message
nocknock  is currently offline nocknock
Messages: 12
Registered: June 2011
Junior Member
Hi friends.

I'm newer on JPA, and i've started with EclipseLink 2.2 and Glassfish 3.1.1.

I have 2 entities, with a relationship ManyToMany between "patient" and "desease", maped from a postgresql database with 3 tables (desease, patient, patient_desase), and "patient_desase" have a foreign key to "desase" that restrict deletes from that table.

My problem is, when i delete a register in table "desease", is deleted on registers in "patient_desease" too. And i don't want that behavior, i don't want that registers on table "patient_desease" be deleted automatically.

What am I doing wrong?

What I want is that when you try to delete a disease suffered by one patient (assigned to this patient on the table "patient_desase"), the erase operation is canceled ( arise and exception for violation of a foreign key), and warning the user that can not be deleted until remove the assignment of this disease to the patient.

I want block erase actions of diseases while they are assigned to patients.


These are the entities.

@Entity
@Table(name = "desease")
@XmlRootElement
public class Desease implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "code")
private Integer code;
@Size(max = 50)
@Column(name = "name")
private String name;

@JoinTable(name = "patient_desease", joinColumns = {@JoinColumn(name = "cod_des",
referencedColumnName = "code")}, inverseJoinColumns = {@JoinColumn(name = "cod_pat",
referencedColumnName = "code")})
@ManyToMany
private List<Patient> patientList;

....
}

@Entity
@Table(name = "patient")
@XmlRootElement
public class Patient implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "code")
private Integer code;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 40)
@Column(name = "name")
private String name;
.....
@ManyToMany(mappedBy = "deseaseList")
private List<Desease> deseaseList;
.....
}


Thank you, very much.

[Updated on: Fri, 03 June 2011 15:05]

Report message to a moderator

Re: Cascade delete not desired, on a ManyToMany relation [message #676305 is a reply to message #676245] Fri, 03 June 2011 15:13 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1034
Registered: July 2009
Senior Member
Hello,

The relationship is writable from the Desease side, so when Desease is removed so are all entries in the patient_desease table since they cannot exist without the owner. Switching the owning side of the Desease<->Patient relationship so that the join table is defined on Patient and the relation in Desease is marked as mappedby the deseaseList will give you the behavior you are looking for.

Best Regards,
Chris
Re: Cascade delete not desired, on a ManyToMany relation [message #676313 is a reply to message #676305] Fri, 03 June 2011 15:47 Go to previous messageGo to next message
nocknock  is currently offline nocknock
Messages: 12
Registered: June 2011
Junior Member
Thanks Chris for your help. But it does not behave as I need, I made ​​the following changes, which I think is what you told me. Not if I was wrong.

These are the new entities.

@Entity
@Table(name = "desease")
@XmlRootElement
public class Desease implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "code")
private Integer code;
@Size(max = 50)
@Column(name = "name")
private String name;
....
@ManyToMany(mappedBy = "deseaseList")
private List<Patient> patientList;

....
}

@Entity
@Table(name = "patient")
@XmlRootElement
public class Patient implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "code")
private Integer code;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 40)
@Column(name = "name")
private String name;
.....

@JoinTable(name = "patient_desease", joinColumns = {@JoinColumn(name = "cod_pat", referencedColumnName = "code")}, inverseJoinColumns = {@JoinColumn(name = "cod_des", referencedColumnName = "code")})
@ManyToMany
private List<Desease> deseaseList;

.....
}

Thank you again.
Re: Cascade delete not desired, on a ManyToMany relation [message #676750 is a reply to message #676313] Mon, 06 June 2011 12:22 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1034
Registered: July 2009
Senior Member
Sorry, how is it behaving now? If a Disease gets deleted, the references to it in the patient_desease should still exist and cause a constraint exception in the database. What database are you using?

Best Regards,
Chris
Re: Cascade delete not desired, on a ManyToMany relation [message #677105 is a reply to message #676750] Tue, 07 June 2011 15:23 Go to previous message
nocknock  is currently offline nocknock
Messages: 12
Registered: June 2011
Junior Member
Now operational. As you told me Chris. Apparently had a problem updating from my Netbeans to Glassfish. Once deployed properly, it works perfectly.

I also seemed odd that does not work correctly.

Many thanks for your help.
Previous Topic:Criteria API and grouping by a truncated date
Next Topic:(no subject)
Goto Forum:
  


Current Time: Thu Oct 23 19:33:07 GMT 2014

Powered by FUDForum. Page generated in 0.02391 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software