Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8049709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:51:31+00:00 2026-06-05T06:51:31+00:00

Firstly clarify that I checked a lot of posts before posting. Maybe I just

  • 0

Firstly clarify that I checked a lot of posts before posting. Maybe I just can’t understand but the solution was posted.

I am developing an struts app v 1.2.9 with hibernate 3 (no spring).
My problem is that if for example I delete an entity, I do commit, then I can see the changes are ok in the database. But, if I query the table, retrieve this entity that I delete in advance.

This is part of my hibernate.cfg.xml

<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">XXXXXXXXXXXXXXX</property>
<property name="hibernate.connection.username">X</property>
<property name="hibernate.connection.password">X</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.hbm2ddl.auto">validate</property>

org.hibernate.cache.NoCacheProvider

My HibernateUtil (copied from other posts)

 import org.hibernate.SessionFactory;

      import org.hibernate.cfg.Configuration;

      public class HibernateUtil {

            private static final SessionFactory sessionFactory;

            static {
                try {
                    // Create the SessionFactory from hibernate.cfg.xml
                sessionFactory = new           Configuration().configure().buildSessionFactory();

                } catch (Throwable ex) {
                    // Make sure you log the exception, as it might be swallowed
                    System.err.println("Initial SessionFactory creation failed." + ex);
                    throw new ExceptionInInitializerError(ex);
                }
            }

            public static SessionFactory getSessionFactory() {
                return sessionFactory;
            }

And my DAO

import com.mylib.modelo.Television;
import com.mylib.plugins.HibernateUtil;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.mylib.plugins.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;


public class TelevisionsDAO  {

   private static final Log log = LogFactory.getLog(TelevisionsDAO.class);

    public Television getTelevisionById(int idTelevision){
        Session session=null;
        Transaction tx =null;
        Television rs =null;
        try{
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            session.flush();

            //QJ: Use Hibernate Criteria for more secured functionalities
            rs=(Television) session.createCriteria(Television.class)
                     .add(Restrictions.eq("idTelevision", idTelevision))
                     .uniqueResult();
            if(rs!=null)session.refresh(rs);
            tx.commit();
        }catch(HibernateException e){
            e.printStackTrace();
        }
         return rs;
    }
    public List<Television> encuentraTelevisions(Integer idUsuario){
        Session session=null;
        Transaction tx =null;
        List rs =null;
        try{
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            //QJ: Use Hibernate Criteria for more secured functionalities
            rs=(List)session.createCriteria(Television.class)
                     .add(Restrictions.eq("usuario.idUsuario", idUsuario))
                     .list();
        }catch(HibernateException e){
            e.printStackTrace();
        }
         return rs;
    }

    public List<Television> encuentraTelevisions(Integer idUsuario, Boolean buscaOfrece){
        Session session=null;
        Transaction tx =null;
        List rs =null;
        try{
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            //QJ: Use Hibernate Criteria for more secured functionalities
            rs=(List)session.createCriteria(Television.class)
                     .add(Restrictions.eq("usuario.idUsuario", idUsuario))
                     .add(Restrictions.eq("buscaOfrece", buscaOfrece))
                     .list();
//            for(int i=0;i<rs.size();i++){
//                session.refresh((Television)rs.get(i));
//            }
            tx.commit();
        }catch(HibernateException e){
            e.printStackTrace();
        }
         return rs;
    }

    public boolean hasTelevision(Integer idUsuario, Boolean buscaOfrece,
            Integer idConocimiento){
        Session session=null;
        Transaction tx =null;
        List rs =null;
        try{
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            //session.flush();
            //QJ: Use Hibernate Criteria for more secured functionalities
            rs=(List)session.createCriteria(Television.class)
                     .add(Restrictions.eq("usuario.idUsuario", idUsuario))
                     .add(Restrictions.eq("buscaOfrece", buscaOfrece))
                     .add(Restrictions.eq("conocimiento.idConocimiento", idConocimiento))
                     .list();
            tx.commit();
        }catch(HibernateException e){
            e.printStackTrace();
        }
         return rs.size()>0;
    }

    public Integer addTelevision(Television television) throws Exception {
        log.info("addTelevision");
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        try {
            Integer p = (Integer) session.save(television);
            session.flush();
            session.refresh(television);
            tx.commit();

            log.info("end addTelevision");
            return p;
        } catch (HibernateException e) {
            log.error("addTelevision", e);
            tx.rollback();
            throw e;
        }
    }

    public void quitarTelevision(Television television) throws Exception {
        log.info("quitarTelevision");

        Session session= HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        try {
            session.delete(television);
            session.flush();
            //session.refresh(television);
            tx.commit();

        } catch (HibernateException e) {
            log.error("quitarTelevision", e);
            tx.rollback();
            throw e;
        } 
    }

In TelevisionBL:

public Integer addTelevision(Usuario usuario, Integer idConocimiento,
boolean buscaOfrece)throws Exception{

    //compruebo que el usuario no contiene la telerencia en cuestión       
    if(!televisionDAO.hasTelevision(
            usuario.getIdUsuario(), buscaOfrece, idConocimiento)){
        Television tel = new Television();
        tel.setBuscaOfrece(buscaOfrece);
        tel.setUsuario(usuario);
        tel.setConocimiento(new Conocimiento(idConocimiento));
        return telerenciasDAO.addTelevision(tel);
    }else{
        return -1;
    }
  }

   public boolean quitarTelevisionSafe(Integer idTelevision, Integer idUsuarioRequesting) throws Exception{
        Television television = televisionsDAO.getTelevisionById(idTelevision.intValue());
        if( television.getUsuario().getIdUsuario().intValue() == idUsuarioRequesting.intValue() ){
            televisionsDAO.quitarTelevision(television);
            return true;
        }else{
            return false;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:51:32+00:00Added an answer on June 5, 2026 at 6:51 am

    At some point you need to close the session.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly, I just want to say that the gmaps4rails gem has been fantastic. Easy
Firstly, let me just say that I'm very new to JSR-170 and Jackrabbit/Lucene in
Firstly yes i know that ints are more native than bytes and that all
Firstly, Kohana's documentation is terrible, before people go read the docs I have read
Firstly, I would like to say that I don't mean the full path, what
Firstly, this is for a class so there are limitations on what we can
Firstly, apologies if I make a lot of mistakes in best VBA practice. I've
Firstly I realise that this is a familiar question, it seems to pop up
Firstly, let me say that I've been on this issue for pretty much 2
Firstly, YES, this is a duplicate issue that's been asked 100 times on here,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.