Data deletion via Hibernate

Asked

Viewed 119 times

2

In the database used by my application I have a table that I defined as temporary that I write some data but at the end of the process I no longer need the information recorded in that table, how to make this table clean would be more useful to carry out a process via database or the application itself should take care of this.?

I use the Hibernate and tried some situations but I can do the em.remove(temp); from a defined object, and not from the whole content.

An example of how I do a database search:

 public EntityManager getEntityManager() {
    return ConexaoJPA.getEntityManager();
}

public List find(int id) {

    EntityManager em = getEntityManager();
    try {

        Query q = em.createQuery("from Temp where cod=" + id + "");

        return (List<Temp>) q.getResultList();

    } finally {
        em.close();
    }
}
  • So, did you succeed? If yes, consider including your solution, you can help other people.

  • I couldn’t, I’m checking the Hibernate documentation to see if I can get something more accurate.

  • What happens to the suggestions below, someone? Can debug and see what’s happening, or provide a reproducible example of what you’re doing?

4 answers

2


If you want to use the remove, you can wear something like that:

Temp temp = new Temp();
temp.setId(1);
getEntityManager().remove(temp);

In this case the object must be in the session, otherwise a org.hibernate.PersistentObjectException: detached entity, something like that, I can’t remember for sure now :)

Using JPQL you can use something like this:

getEntityManager()
    .createQuery("delete from Temp t where t.id = :id")
    .setParameter("id", id)
    .executeUpdate();

1

Try this:

public int truncate(String nomeTabela){
    Query query = session.createSQLQuery('truncate table ' + nomeTabela);
    return query.executeUpdate();
}

`

  • I will update the question to specify better how I use today. Because I don’t Session and yes Entity Manager.

  • Already tried: Query query = manager.createNativeQuery()?

  • I have tried but without success.

  • Strange not working. See this post: http://stackoverflow.com/questions/12041547/truncate-table-in-play-framework-jpa-hibernate

1

@Resource
UserTransaction utc;

@PersistenceContext(unitName = "xyz")
EntityManager em;

try {
    utx.begin();

    Query q1 = em.createNativeQuery("DELETE FROM tabela");

    q1.executeUpdate();

    utc.commit();
} catch (NotSupportedException | SystemException | SecurityException | IllegalStateException | RollbackException | HeuristicMixedException | HeuristicRollbackException e) {
    e.printStackTrace();
}
  • also unsuccessfully your option.

  • Throw an error? you are opening a transaction before deleting?

  • makes no mistake.

  • and the transaction? post what you tried using em.createNativeQuery

-1

I know this post is old, but I think I can help. Here’s an example of how to delete:

@Stateful
@Local (EmpresaLocal.class)
@Remote (EmpresaRemote.class)
@RemoteBinding( jndiBinding="EmpresaBean/remote" )
@LocalBinding( jndiBinding="EmpresaBean/local" )
@TransactionManagement(TransactionManagementType.BEAN)
public class EmpresaBean  { 
    private List<Empresa> empresas; 
    
    @PersistenceUnit(unitName="Curriculo")  
    private EntityManagerFactory emf=null;
    
    @PersistenceContext(unitName = "Curriculo")  
    private EntityManager entitymanager= null;   
     
    private UserTransaction transaction = null;
        
    public EmpresaBean() { 
        empresas = new ArrayList<Empresa>();   
        this.emf =  Persistence.createEntityManagerFactory("Curriculo");
        this.entitymanager=emf.createEntityManager();
        try {
            this.transaction = (UserTransaction) new InitialContext().lookup("UserTransaction");
        } catch (Exception e) {         
            e.printStackTrace();
        } 
    } 

 public void remove(Empresa empresa) {
        try {
            transaction.begin();            
            entitymanager.remove(entitymanager.merge(empresa));
            entitymanager.flush();
            transaction.commit();
        } catch (Exception e) {         
            try {
                transaction.rollback();
            } catch (IllegalStateException e1) {
                e1.printStackTrace();
            } catch (SecurityException e1) {
                e1.printStackTrace();
            } catch (SystemException e1) {
                e1.printStackTrace();
            }           
            e.printStackTrace();
            
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.