2

I am trying to delete Entities in a JPA/Hibernate configuration. The database is an embedded HSQL file.

This is code doing the delete:

        EntityManager em = PersistenceUtility.getInstance().createEntityManager();
        EntityTransaction t = em.getTransaction();
        t.begin();

        List<ServiceTicket> tickets = em.createQuery("from ServiceTicket").getResultList();

        for (ServiceTicket ticket : tickets) {
            Report report = ticket.getReport();
            em.merge(report);
            em.merge(ticket);
            em.remove(report);
            em.remove(ticket);
        }

        em.flush();
        t.commit();

The ServiceTicket references the entitiy Report. This is why I had to remove Report first.

I do not get an error in this context. But the changes are not written to the database.

Here is the configuration of the persistence unit:

<persistence-unit name="local_hsql" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Report</class>
<class>ServiceTicket</class>
<properties>
  <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
  <property name="hibernate.connection.url" value="jdbc:hsqldb:file:etc/database/db1"/>
  <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
  <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
  <property name="hibernate.show_sql" value="false"/>
  <property name="hibernate.connection.SetBigStringTryClob" value="true"/>
  <property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>

What am I doing wrong?

Thanks in advance and regards from Germany

Marco

1
  • 1
    Acouple of comments: There is no need to merge the entites, they are already managed. Also keep in mind that merge returns a managed entity but does nothing to the object passed as argument. There is no need to flush, since you commit the changes on the line after. Another (simpler) approach would to be declare the mapping of the ServiceTicket as cascade= CascadeType.REMOVE (or ALL) in Report, and only call em.remove() on the Report. Maybe there is something wrong with the EntityManager. Does anything ever work with this em? (as in does it find Reports in this code snippet?) (turn on show sql) Commented Apr 27, 2012 at 9:52

1 Answer 1

1

you can use

@OneToOne(cascade = CascadeType.REMOVE)

It will delete Report when you remove ServiceTicket.

Second: If you want to delete all ServiceTickets where something equals x you can use HQL:

Query query = session.createQuery("delete ServiceTicket where something = :x");
query.setParameter("x", "myvalue");
int result = query.executeUpdate();
Sign up to request clarification or add additional context in comments.

1 Comment

@MarcoNätlitz, It doesn't matter, you still can use HQL with and CascadeType.Remove with EntityManager. I just wrote you example of what should be done. Just implement it using EntityManager.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.