More problems with JPA deletion of row in database

The name of the picture


More problems with JPA deletion of row in database



I have the following code that defines the relationship between three tables.



public class Attachment implements Serializable {


@Id
@Column(name="attachment_id")
private int attachmentId;

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="reference_id")
private Reference reference;

@OneToMany(mappedBy="attachment")
private List<Reference> references;

MORE STUFF;



}



public class Uuid implements Serializable {


@Id
@Column("name=uuid_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int uuidId;

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="reference_id")
private Reference reference;

MORE STUFF



}



public class Reference implements Serializable {


@Id
@Column(name="reference_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int referenceId;

@OneToMany(mappedBy="reference")
private List<Attachment> attachments;

@ManyToOne(cascade=CascadeType.MERGE)
@JoinColumn(name="attachment_id")
private Attachment attachment;

@OneToMany(mappedBy="reference")
private List<Uuid> uuids;

MORE STUFF



}



I have some more code that picks the specific "uuid" object/row that needs to be deleted, and the idea is that anything in the other tables that needs deleting because they share the same reference_id should be deleted too. The code that does this is:



try {



final EntityTransaction transaction = em.getTransaction();



transaction.begin();



em.remove(data);



transaction.commit();
}



catch (final PersistenceException e) {


throw new CPDPersistenceException(e);



}



When the delete is performed it throws Exception "Cannot delete or update parent row: a foreign key constraint fails. I posted something on a variation of this before. Does anybody have any ideas? Thanks for your time.





What is data in your snippet?
– Mick Mnemonic
1 min ago


data









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

Future solutions