Hibernate object problem!

2

Good morning, everyone! I have a problem with the objects in Hibernate.

This is the method I use to extract the object:

public Moto loadMotoId(SessionFactory SessionBuilder, int idMoto) {
    Session conexion = SessionBuilder.openSession();

    Query q = conexion.createQuery("FROM Moto WHERE id = :_nid");
    q.setParameter("_nid", idMoto);

    List listMotos = q.list();

    Moto moto = new Moto();

    if (!listMotos.isEmpty()) {
        moto = (Moto) listMotos.get(0);
    } else {
        listMotos = null;
    }
    conexion.close();

    return moto;
}

This is part of the servlet code:

HttpSession session = request.getSession(true);
Operaciones objop = new Operaciones();    
Moto moto=objop.loadMotoId(SessionBuilder, idMoto);
session.setAttribute("moto", moto);

When I want to show the different attributes of the object in the jsp, the id fields related to other tables do not show them to me, for example:

I pick up the object previously saved in the servlet in the session variable:

<%
Moto moto = (Moto) session.getAttribute("moto");
%>

Part of the html code of a table where I want to show the type of motorbike of the motorbike object, which is a key to another table in my bbdd:

<td><%=moto.getTipoMoto().getNombre()%></td>

Well, in that line I get an error, the rest of the fields shows them correctly except for those that are foundational keys to other tables and I do not know why it is. Let's see if you can give me a cable. Thanks !!

I add the Moto class here:

package POJO;
// Generated 30-ene-2018 13:25:55 by Hibernate Tools 4.3.1


import java.util.HashSet;
import java.util.Set;

/**
 * Moto generated by hbm2java
 */
public class Moto  implements java.io.Serializable {


 private Integer id;
 private Diseno diseno;
 private TipoMoto tipoMoto;
 private String marca;
 private String modelo;
 private int cilindrada;
 private int caballos;
 private float aceleracion;
 private float consumo;
 private float velocidadMax;
 private float precioBase;
 private String imagen;
 private int stock;
 private Set reservas = new HashSet(0);

public Moto() {
}


public Moto(Diseno diseno, TipoMoto tipoMoto, String marca, String modelo, int cilindrada, int caballos, float aceleracion, float consumo, float velocidadMax, float precioBase, String imagen, int stock) {
    this.diseno = diseno;
    this.tipoMoto = tipoMoto;
    this.marca = marca;
    this.modelo = modelo;
    this.cilindrada = cilindrada;
    this.caballos = caballos;
    this.aceleracion = aceleracion;
    this.consumo = consumo;
    this.velocidadMax = velocidadMax;
    this.precioBase = precioBase;
    this.imagen = imagen;
    this.stock = stock;
}
public Moto(Diseno diseno, TipoMoto tipoMoto, String marca, String modelo, int cilindrada, int caballos, float aceleracion, float consumo, float velocidadMax, float precioBase, String imagen, int stock, Set reservas) {
   this.diseno = diseno;
   this.tipoMoto = tipoMoto;
   this.marca = marca;
   this.modelo = modelo;
   this.cilindrada = cilindrada;
   this.caballos = caballos;
   this.aceleracion = aceleracion;
   this.consumo = consumo;
   this.velocidadMax = velocidadMax;
   this.precioBase = precioBase;
   this.imagen = imagen;
   this.stock = stock;
   this.reservas = reservas;
}

public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}
public Diseno getDiseno() {
    return this.diseno;
}

public void setDiseno(Diseno diseno) {
    this.diseno = diseno;
}
public TipoMoto getTipoMoto() {
    return this.tipoMoto;
}

public void setTipoMoto(TipoMoto tipoMoto) {
    this.tipoMoto = tipoMoto;
}
public String getMarca() {
    return this.marca;
}

public void setMarca(String marca) {
    this.marca = marca;
}
public String getModelo() {
    return this.modelo;
}

public void setModelo(String modelo) {
    this.modelo = modelo;
}
public int getCilindrada() {
    return this.cilindrada;
}

public void setCilindrada(int cilindrada) {
    this.cilindrada = cilindrada;
}
public int getCaballos() {
    return this.caballos;
}

public void setCaballos(int caballos) {
    this.caballos = caballos;
}
public float getAceleracion() {
    return this.aceleracion;
}

public void setAceleracion(float aceleracion) {
    this.aceleracion = aceleracion;
}
public float getConsumo() {
    return this.consumo;
}

public void setConsumo(float consumo) {
    this.consumo = consumo;
}
public float getVelocidadMax() {
    return this.velocidadMax;
}

public void setVelocidadMax(float velocidadMax) {
    this.velocidadMax = velocidadMax;
}
public float getPrecioBase() {
    return this.precioBase;
}

public void setPrecioBase(float precioBase) {
    this.precioBase = precioBase;
}
public String getImagen() {
    return this.imagen;
}

public void setImagen(String imagen) {
    this.imagen = imagen;
}
public int getStock() {
    return this.stock;
}

public void setStock(int stock) {
    this.stock = stock;
}
public Set getReservas() {
    return this.reservas;
}

public void setReservas(Set reservas) {
    this.reservas = reservas;
}

}

    
asked by Ganejash 01.02.2018 в 17:16
source

1 answer

2

Surely the problem you have is probably the following scenario:

  • You ask Hibernate for a session.
  • You ask for a Moto session.
  • Hibernate creates a Moto instance by making an SQL query to the database. This instance of Moto points to other elements that would have to be recovered from other tables, but instead of making all those queries, Hibernate stops here because it is configured to work in mode " FetchType.LAZY " (type of obtaining "LAZY"). Instead of bringing those objects to you, create special ones to detect when you need them and get them later.
  • You close the session (no connection, you're messing around there)
  • You try to obtain in your JSP the values that have not been obtained. Hibernate tries to request them but the session has been closed, so you can not get them and throws a "LazyInitializationException" bug.
  • Solutions:

  • Use FetchType.EAGER to obtain that data always when requesting a Moto object to the database.
  • Tell Hibernate that you can get that data out of a transaction, which is not a problem to obtain later, adding the hibernate.enable_lazy_load_no_trans = true option to your connection configuration.
  • The first solution has two ways to implement:

    The modern , that is, using annotations:

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "tipo_moto_id") //columna de la tabla moto que hace de clave foránea
    private TipoMoto tipoMoto;
    

    The classic one, using a hbm.xml file:

    <many-to-one name="tipoMoto" class="POJO.TipoMoto" lazy="false">
        <column name="tipo_moto_id" />
    </many-to-one>
    

    Extra tip, outside the context of the question: Avoid using scriptlets in a JSP, they are a cancer!

        
    answered by 01.02.2018 / 17:39
    source