1

I have an entity, PersonEntity, with an id (PERSON_ID column), a partition key (TENANT_ID column) and a collection of AddressEntity. The AddressEntity has an id (ADDRESS_ID), a partition key (TENANT_ID), and a field (VALUE).

This is the way it looks in the PersonEntity:

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "person", orphanRemoval = true)
public Set<AddressEntity> getAddresses() {
    return addresses;
}

Since CascadeType.ALL is set, when I a delete a person, it triggers a delete for the address as well. I use the Session's delete(Object) method to delete the person.

The resulting SQL for that delete looks like this:

delete from address where address_id=?
delete from person where person_id=?

Is there any ORM way to perform this cascaded delete using both the person_id and tenant_id such that I get the following SQL generated:

delete from address where address_id=? and tenant_id=?
delete from person where person_id=? and tenant_id=?

I know I could simply write all the individual deletes using SQL, but I like the simplicity of using Hibernate to take care of all the associated table deletions.

The reason I want to do this is to ensure that the delete does not have to loop through partitions to find the address_id or person_id. If I gave it the tenant_id, it would be able to instantly go to the correct partition.

AHungerArtist
  • 9,332
  • 17
  • 73
  • 109

1 Answers1

1

Take a look at this answer for a more general overview of difficulties around using table partitioning with Hibernate.

For your use case, using a composite id in which tenant id is also included seems to be the only possible solution.

Community
  • 1
  • 1
Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110
  • This is unfortunate, but I guess it's not so bad, as there really is no use case for cross-partition queries in our business workflow. Just will take some extra work -- thanks. – AHungerArtist Mar 06 '17 at 16:45