I'm trying to remove a self-referencing object from my system but the below workflow doesn't allow it:
pseudo code:
-client wants to delete language object (Korean) from the system (the language object's language is itself, Korean)
-client sets object to deleted and calls save(true, languageObject) in my persistence layer
-begin transaction
-set language object's language to English (English will always exist in the DB)
-persist the object with the above change (to remove self reference)
-delete the object (no object, including itself, has a language of Korean now)
-commit the transaction
Code:
public void save(boolean commit, ValueObject object) {
if (commit) {
_session.getTransaction().begin();
}
if (object.isDeleted()) {
preDelete(object);
_session.delete(object);
} else {
_session.persist(object);
}
if (commit) {
_session.getTransaction().commit();
}
}
private void preDelete(ValueObject valueObject) {
if (valueObject instanceof Language) {
Language sl = (Language) valueObject;
sl.setLanguage((LanguageDO) get(LanguageId.EN));
sl.setDeleted(false);
save(false, sl);
sl.setDeleted(true);
}
}
This results in a foreign key violation saying the language object itself still has a self-referencing language. I would expect the commit to execute the update first and the delete second. Thus the commit should run clean. Am I misunderstanding how Hibernate works in this fashion? Is there a better way to do this?
And I need to run this as one single transaction so that if there are any errors, I can rollback the entire transaction. So I can't begin a transaction, update the object, commit and then begin, delete, commit.
Thanks!
Shawn