0

in the application that I am working on, somebody split the domain objects into different persistence units. The connection between tables from different persistence units is made based on columns containing the ID for instance:

TableA             TableB
ID:Int             ID:int
Field1             TableA_ID:Int
Field2             ...  
...

The TableA_ID is just a plain int, no constraints are enforced on it. Now I need to delete the parent entity (let's say TableA but also all the connected TableBs entities). In case of a foreign key relation, this would be a plain and simple CASCADE delete. But in this case, how should I proceed? A DB Trigger - I would like to keep everything in JPA.

Thank you!

Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49

1 Answers1

0

If you have DAOs for TableA and TableB you can achieve a solution writing a service method using collaboration in this manner (just a sample):

@Repository
class TableADAO {
  void deleteById(int id) {
    // perform deletion where TableA.ID == id
  }
}

@Repository
class TableBDAO {
  void deleteTableAChildren(int id) {
    // perform deletion where TableA_ID == id
  }
}

@Service
class MyService {
  private TableADAO tableADAO;
  private TableBDAO tableBDAO;

  @Transactional
  // Annotating as transactional to prevent
  // invalid data
  void deleteTableACascade(int idTableA) {
    tableBDAO.deleteTableAChildren(idTableA);
    tableADAO.deleteById(idTableA);
  }
}

Is it a plausible solution?

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
  • thank you for your response, yes, this would be the most common sense approach. Unfortunately the persistence level is written in a very generic fashion, so composing DAOs can be at least complicated. This is why I was looking more for a solution based on triggers/ events. – Olimpiu POP Aug 09 '13 at 05:56
  • ok, but I think that this logic can be applicated in every situation: instead of using dao just do raw SQL in service method, I think @Transactional() is the point of interest, not the way you use to delete data. my 2 cents – Luca Basso Ricci Aug 09 '13 at 06:00
  • I finally solved the issue by using CDI events. Thank you for your support. – Olimpiu POP Aug 09 '13 at 11:22