1
votes

I need no update Values when update Crypto, but delete all child Values when delete Crypto:

class Crypto {
...
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name="cryptoId", updatable=false)
    private Set<Values> values;
...
}


class Values {
...
    @ManyToOne
    @JoinColumn(name = "cryptoId")
    private Crypto crypto;
...
}

SQL debug when I delete Crypto:

Hibernate: delete from "Crypto" where "id"=?

Error:

java.sql.SQLIntegrityConstraintViolationException: ORA-02292: integrity constraint (ARTIKUA.FK_8R5C0WLWJ3LIJTVJ559EV8NAT) violated - child record found

DB is Oracle Why Hibernate, before delete "Crypto", don't delete all their child "Values" values?

1
How are you deleting the Crypto object instance? via HQL? - Andy Dufresne
session = sf.openSession(); session.delete(); session.close(); - Artik
Can you paste your complete code? It is strange that session.delete(crypto) is not deleting the child instances. Do you load the crypto object before delete? - Andy Dufresne
Crypto cry = session.get(Crypto.class, 5); session.delete(cry); - Artik

1 Answers

1
votes

If you are deleting the Crypto object through hql, hibernate cannot not cascade the delete operation. Since the cascade is a hibernate configuration matter (i.e. hbm.xml or annotations) the data must be returned to Java for processing of cascades. Doing UPDATE/DELETE row modifications in HQL happens entirely on the database server.

What you can do:

  1. fetch all Crypto instances that should be removed. for each of them.
  2. Call entityManager.remove() or session.delete().