Hello,
I have to manage the following classes through JPA API manage by org.hibernate.ejb.HibernatePersistence [ version 3.5.1]:
Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="disc",discriminatorType=DiscriminatorType.STRING)
public abstract class Root {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
}
[...]
@Entity
@DiscriminatorValue("A")
public class A extends Root {
@Entity
public static class B {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Basic
private String name;
}
private String name;
@OneToMany(orphanRemoval = true, cascade = CascadeType.ALL)
@JoinColumn(name="fk_a", nullable=false)
private Set<B> bs;
}
[...]
As a summary, A extends Root; A contains an inner class B; A has an unidirectional one-to-many relationship with B. Obviously, I did not mention all getters/setters.
I want to purge all A objects with the following code:
Code:
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from A").executeUpdate();
em.getTransaction().commit();
And Hibernate pushed the folling orders to the DB (PG9):
Code:
Hibernate: insert into HT_A select a0_.id as id from cde.A a0_ inner join cde.Root a0_1_ on a0_.id=a0_1_.id
Hibernate: delete from cde.A where (id) IN (select id from HT_A)
0 [main] ERROR org.hibernate.util.JDBCExceptionReporter - ERREUR: UPDATE ou DELETE sur la table « a » viole la contrainte de clé étrangère
« fkf89f26e3276c » de la table « a$b »
Détail : La clé (id)=(1) est toujours référencée à partir de la table « a$b ».
So my first question is why Hibernate does not care about the B instances related to A objects?
Second one is why Hibernate needs to create a temp table to store the ids to delete?
Regards