Problem with removing in OneToMany relationship

View: New views
2 Messages — Rating Filter:   Alert me  

Problem with removing in OneToMany relationship

by jalvarez :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello everybody, I've two entities mapped as follows:

Entity Patient:

     ...
     @Id
     private Long id;
     ...
     @OneToMany(cascade=CascadeType.ALL)
     @JoinTable(name="PATIENT_SESSION",
        joinColumns={@JoinColumn(name="patient_fk")},
        inverseJoinColumns={@JoinColumn(name="session_fk")}
     )
     private Collection<Session> sessions;
     

and Entity Session:

     ...
     @Id
     private Long id;
     ...
   
This generates a PATIENT table, a SESSION table and a PATIENT_SESSION table. Persist and Update works fine, but when i try to remove(a_patient), i get the following error (in spanish :P):

Internal Exception: java.sql.SQLIntegrityConstraintViolationException: DELETE en la tabla 'SESSION' ha causado una violación de restricción de clave foránea 'PTENTESESSIONSSONFK' para la clave (1).

Is this behavior a known bug or there is a workaround for this, or another way to map correctly the entities?

Re: Problem with removing in OneToMany relationship

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

What JPA provider and version are you using?  It seems the deletion is occuring in the wrong order for your foreign key constraints.  One generic workaround may be to remove the objects from the OneToMany before deleting the object.
LightInTheBox - Buy quality products at wholesale price!