简体   繁体   中英

hibernate delete second level jpa2.0

I need in my application to remove all data from a cachable table. I suposed that to delete all contents, I had to remove the second level cache, then use a truncate.

@Entity
@Table(name = "\"cpf_formacode\"")
@Cacheable
public class CpfRefFormaCode implements Serializable {
.......
}

the Dao method:

public void deleteAll() {
   SessionFactory sf = em.unwrap(SessionFactory.class);
   sf.getCache().evictEntityRegion(CpfRefFormaCode.class);
   em.createNativeQuery("TRUNCATE TABLE cpf_formacode").executeUpdate();
}

persistence file:

       <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="org.hibernate.FlushMode" value="commit" />
            <!-- property name="hibernate.hbm2ddl.auto" value="create-drop" / -->
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.cache.use_second_level_cache" value="true" />
            <property name="hibernate.cache.use_query_cache" value="true" />
            <property name="hibernate.cache.infinispan.cachemanager" value="java:jboss/infinispan/hibernate" />
            <property name="hibernate.cache.region.factory_class" value="org.jboss.as.jpa.hibernate4.infinispan.InfinispanRegionFactory" />
            <property name="hibernate.cache.infinispan.statistics" value="true" />
        </properties>

the error i have :

17:50:17,161 ERROR [org.jboss.as.ejb3.tx.CMTTxInterceptor] (http--127.0.0.1-8080-2) javax.ejb.EJBTransactionRolledbackException: Hibernate cannot unwrap interface org.hibernate.SessionFactory
17:50:17,163 ERROR [org.jboss.ejb3.invocation] (http--127.0.0.1-8080-2) JBAS014134: EJB Invocation failed on component CpfRefFormaCodeDao for method public void com.agefos.corp.business.dao.CpfRefFormaCodeDao.deleteAll(): javax.ejb.EJBTransactionRolledbackException: Hibernate cannot unwrap interface org.hibernate.SessionFactory
Caused by: javax.persistence.PersistenceException: Hibernate cannot unwrap interface org.hibernate.SessionFactory

I finished by findig the solution, The problem that i was trying to clean the cach before deleting the data, and it was not the best practice

public void deleteAll() {
    try {
        TypedQuery<MyEntity> query = em.createQuery("From MyEntity f", MyEntity.class);
        query.setHint("org.hibernate.cacheable", true);
        List<MyEntity> result = null;
        result = query.getResultList();
        if (!result.isEmpty()) {
            for (MyEntity f : result) {
                em.remove(f);
            }
        }
        em.flush();

    } catch (Exception e) {
        throw new PersistanceException("An error occurred while attempting to delete an instance of an object : " + entityClass, e);
    }
}

the problem was resolved by adding the

 em.flush();

In other words, flush tells Hibernate to execute the SQL statements needed to synchronize the JDBC connection's state with the state of objects held in the session-level cache. so i was abel to save other entities without ID problems

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM