Error org.hibernate.LazyInitializationException how to resolve?

2

I know this error is derived from trying to fetch some data from the database, when I do not have any open session.

But the biggest problem for me is that this error is appearing to me completely random, as I am developing a server that processes data and then sends it to clients, every 3 minutes the server repeats the same operations, but the error both appears as soon as the server is running as it may appear after 3 or 4 hours of running the server.

The error always points to class Produto more precisely for method toString , I commented the method and now it gives error in hashCode .

Database structure:

|| Lot || ---- Contain --- > || Product || ---- Contain --- > || qualityGates ||

Code - > Product.java

public class Produto  implements java.io.Serializable {


     private Integer idProduto;
     private Qualitygate qualitygateByIdQualityGate3;
     private Qualitygate qualitygateByIdQualityGate2;
     private Qualitygate qualitygateByIdQualityGate1;
     private Familiaproduto familiaproduto;
     private String noproduto;
     private Integer vt;
     private String tipo;
     private Set loteestados = new HashSet(0);

    public Produto() {
    }

//++ GET'S & SET'S

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 59 * hash + Objects.hashCode(this.idProduto);
        hash = 59 * hash + Objects.hashCode(this.qualitygateByIdQualityGate3); // <-----erro
        hash = 59 * hash + Objects.hashCode(this.qualitygateByIdQualityGate2);
        hash = 59 * hash + Objects.hashCode(this.qualitygateByIdQualityGate1);
        hash = 59 * hash + Objects.hashCode(this.familiaproduto);
        hash = 59 * hash + Objects.hashCode(this.noproduto);
        hash = 59 * hash + Objects.hashCode(this.vt);
        hash = 59 * hash + Objects.hashCode(this.tipo);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Produto other = (Produto) obj;
        if (!Objects.equals(this.idProduto, other.idProduto)) {
            return false;
        }
        if (!Objects.equals(this.qualitygateByIdQualityGate3, other.qualitygateByIdQualityGate3)) {
            return false;
        }
        if (!Objects.equals(this.qualitygateByIdQualityGate2, other.qualitygateByIdQualityGate2)) {
            return false;
        }
        if (!Objects.equals(this.qualitygateByIdQualityGate1, other.qualitygateByIdQualityGate1)) {
            return false;
        }
        if (!Objects.equals(this.familiaproduto, other.familiaproduto)) {
            return false;
        }
        if (!Objects.equals(this.noproduto, other.noproduto)) {
            return false;
        }
        if (!Objects.equals(this.vt, other.vt)) {
            return false;
        }
        if (!Objects.equals(this.tipo, other.tipo)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return " noproduto=" + noproduto;
    }
//    @Override
//    public String toString() {
//        return "Produto{" + "idProduto=" + idProduto + ", qualitygateByIdQualityGate3=" + qualitygateByIdQualityGate3 + ", qualitygateByIdQualityGate2=" + qualitygateByIdQualityGate2 + ", qualitygateByIdQualityGate1=" + qualitygateByIdQualityGate1 + ", familiaproduto=" + familiaproduto + ", noproduto=" + noproduto + ", vt=" + vt + ", tipo=" + tipo + ", loteestados=" + loteestados + '}';
//    }

  }

Code - > Product.hbm.xml

<hibernate-mapping>
  <class catalog="mydb" name="Models.Produto" table="produto">
    <id name="idProduto" type="java.lang.Integer">
      <column name="idProduto"/>
      <generator class="identity"/>
    </id>
    <many-to-one class="Models.Qualitygate" fetch="select" name="qualitygateByIdQualityGate3">
      <column name="idQualityGate3"/>
    </many-to-one>
    <many-to-one class="Models.Qualitygate" fetch="select" name="qualitygateByIdQualityGate2">
      <column name="idQualityGate2"/>
    </many-to-one>
    <many-to-one class="Models.Qualitygate" fetch="select" name="qualitygateByIdQualityGate1">
      <column name="idQualityGate1"/>
    </many-to-one>
    <many-to-one class="Models.Familiaproduto" fetch="select" name="familiaproduto">
      <column name="idNomeProduto"/>
    </many-to-one>
    <property name="noproduto" type="string">
      <column length="45" name="noproduto" not-null="true" unique="true"/>
    </property>
    <property name="vt" type="java.lang.Integer">
      <column name="vt"/>
    </property>
    <property name="tipo" type="string">
      <column length="10" name="tipo"/>
    </property>
    <set fetch="select" inverse="true" lazy="true" name="loteestados" table="loteestado">
      <key>
        <column name="ProdutoidProduto" not-null="true"/>
      </key>
      <one-to-many class="Models.Loteestado"/>
    </set>
  </class>
</hibernate-mapping>

Code - > Generation Session creation (At first I had HibernateUtil to create me the sessions but due to the problem of creating many sessions and giving the full memory error I changed to this approach)

public class GenericDao<T extends Serializable> {

    private static Session session;
    public Class<T> persistentClass;

    public GenericDao(){
        //this.session = HibernateUtil.getSession();
        this.persistentClass = (Class<T>) ((ParameterizedType) 
            getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    public Session createSession(){
            Configuration cfg= new Configuration().configure();
        SessionFactory sessionfactory=cfg.buildSessionFactory();
        session=sessionfactory.openSession();

        return this.session;
    }
    public Session getSession() {
        if(this.session == null || !this.session.isOpen()){  
           return createSession();
        } else return session;
    }

    protected void save(T entity) {

         try {

            Session session = getSession();
            session.beginTransaction();
            session.save(entity);
            session.getTransaction().commit();
        } catch (Exception e) {
            session.getTransaction().rollback();

        } finally {
            session.close();
        }

   }

// ----------------------- +outros métodos 

    public T findById(Integer id) {
        return (T) getSession().createCriteria(persistentClass)
                .add(Restrictions.eq("id", id)).uniqueResult();
    }


    private void close() {
        if (getSession() != null && getSession().isOpen()) {
            getSession().close();
        }
    }
}

Any solutions to this problem?

    
asked by anonymous 17.11.2014 / 15:45

1 answer

1

First of all, if you always use access all object attributes, you should use eager mode and not lazy mode.

However, the problem may be due to the fact that under certain circumstances Hibernate creates lazy entity proxies, which means that it does not load the data until it intercepts a call to a getter method.

I've had trouble accessing attributes on these object types myself. Solution? Try Call getter instead of directly accessing the attributes.

For example, where do you do this:

hash = 59 * hash + Objects.hashCode(this.idProduto);

You can switch to this:

hash = 59 * hash + Objects.hashCode(this.getIdProduto());
    
17.11.2014 / 16:08