Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

JPA: composite key and deleting records problem

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

here my problem:
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:


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
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.
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic