• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA: composite key and deleting records problem

 
Fabrizio Pia
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

here my problem:
JPA
I've a composite key (4 columns in the table are my key) and related "embeddable" class (named "RelazId") with 4 properties.



Another class (named "Relaz") has RelazId as its own ID key.



Everything seems to work but:

As I wrote above I've 4 columns in the RELAZ table. I need to select all the records in which three columns have fixed values.
Something like SELECT * FROM RELAZ WHERE col1 = x AND col = Y and col3 = Z. All the values in the forth column are good.

In order to work with JPA objects, I need to make a RelazId object and selecting all the Relaz objects.

For example:


Then:


I don't want to set the fourth column because I want to delete all the records with fixed values just on the first three columns.
But obviously java writes that an object RelazId with just three setted properties is not good.

Which is the right way in oder to select just some records based on a columns subset?

Thank you
Fabrizio
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The EntityManager.remove() API in JPA will delete that single object. If you want to delete multiple objects, you can either use a delete query, or select all the objects you want to delete and remove them one by one.

To create a delete query using JPQL you use:


Or create a select query to find all the objects then remove one by one.

If this is something you always want to do when removing this Entity, you may also consider database triggers, or JPA events.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic