2016-04-14 73 views
0

Hibernate ile ilgili bir sorunum var: İki kişiden biri Libro'ya ve Çok-bir ilişkisine sahip. Ben görünen bu hatayı devam çalıştığınızda: Diğer sorularda okurkenHazırda bekleme özelliği başvurular null

Hibernate: select editorial_.ID_EDITORIAL, editorial_.NOMBRE as NOMBRE4_, editorial_.ID_DIRECCION as ID3_4_, editorial_.NIF as NIF4_ from EDITORIAL editorial_ where editorial_.ID_EDITORIAL=? org.hibernate.PropertyValueException: not-null property references a null or transient value: app.modelo.Libro.editorial at org.hibernate.engine.Nullability.checkNullability(Nullability.java:101) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:313) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:144) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:693) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:689) at app.persistencia.LibrosDAO.altaLibro(LibrosDAO.java:41) at app.negocio.GestionLibreria.altaLibro(GestionLibreria.java:26) at app.cliente.Main.main(Main.java:98) Exception in thread "main" org.hibernate.SessionException: Session was already closed at org.hibernate.impl.SessionImpl.close(SessionImpl.java:312) at app.persistencia.LibrosDAO.altaLibro(LibrosDAO.java:49) at app.negocio.GestionLibreria.altaLibro(GestionLibreria.java:26) at app.cliente.Main.main(Main.java:99)

Ben Editoryal Dış Key ile ilgili bir sorun olduğunu düşünüyorum ama gerçekten bulup hatayı çözemez. İşte kodu:

YAYIN HARİTALANDIRMA

<?xml version="1.0"?> 

<class name="app.modelo.Editorial" table="EDITORIAL"> 

    <!-- Clave Primaria --> 

    <id name="ID_editorial" column="ID_EDITORIAL" type="int" > 
     <generator class="assigned" /> 
    </id> 


    <!-- Propriedades --> 

    <property name="nombre" column="NOMBRE" type="string" lazy="false" not-null="false"/> 

    <many-to-one unique="true" name="direccion" column="ID_DIRECCION" not-null="true" cascade="all"/> <!-- Simulamos un Many to One con la entidad Editorial para generar una Foreing Key --> 

    <property name="nif" column="NIF" type="string" lazy="false" not-null="false"/> 

    <set name="libros" cascade="all" > 
     <key column="ID_EDITORIAL" /> 
     <one-to-many class="app.modelo.Editorial" /> 
    </set> 


</class> 

LIBRO HARİTALAMA

<hibernate-mapping> 

<class name="app.modelo.Libro" table="LIBROS"> 

    <!-- Clave Primaria --> 

    <id name="ID" column="ID_LIBRO" type="int" > 
     <generator class="assigned" /> 
    </id>  

    <!-- Propriedades --> 

    <property name="titulo" type="string" column="TITULO" lazy="false"/> 

    <property name="isbn" type="string" column="ISBN" lazy="false"/> 

    <property name="publicacion" type="int" column="PUBLICACION" lazy="false"/> 

    <property name="precio" type="double" column="PRECIO" lazy="false"/> 

    <property name="descripcion" type="string" column="DESCRIPCION" lazy="true" /> 

    <many-to-one name="editorial" column="ID_EDITORIAL" class="app.modelo.Editorial" not-null="true" /> 

    <set name="autores" inverse="true" table="AUTOR_LIBRO" cascade="all"> <!-- Gestionamos el Many to Many con AUTOR haciendo referencia a la tabla intermedia --> 
     <key column="ID_LIBRO" /> <!-- Nueva columna en la tabla intermedia --> 
     <many-to-many column="ID_AUTOR" class="app.modelo.Autor" /> 
    </set> 

</class> 

SINIF LIBROSDAO

public class LibrosDAO implements ItfzLibrosDao { 

SessionFactory sf = new Configuration().configure("hibernate2.cfg.xml").buildSessionFactory(); 

Session session = sf.openSession(); 

Transaction tx = session.getTransaction(); 

/* 
* Crea un nuevo registro en la tabla con los datos del libro recibido como 
* argumento 
*/ 

public boolean altaLibro(Libro libro) { 
    // Creo una variable de tipo boolean que me retorna si se ha podido o no 
    // insertar un nuevo libro 
    boolean insertado = false; 

    Libro l = new Libro(libro.getID(),libro.getTitulo(),libro.getAutores(),libro.getEditorial(),libro.getIsbn(), 
      libro.getPublicacion(),libro.getPrecio(),libro.getDescripcion()); 

    try { 
     tx.begin(); 

     session.save(l); 
     tx.commit(); 
     insertado= true;  
     } catch(Exception ex) { 
      tx.rollback(); 
      ex.printStackTrace(); 
      insertado = false; 
     } finally { 
      session.close(); 

     } 

     return insertado; 
} 

GESTIONLIBRERIA SINIF

public class GestionLibreria implements ItfzGestionLibreria { 

LibrosDAO dao = new LibrosDAO(); 

public boolean altaLibro(Libro libro) { 

    int ID = libro.getID(); 
    String titulo = libro.getTitulo(); 
    Set<Autor> autores = libro.getAutores(); 
    Editorial editorial = libro.getEditorial(); 
    String isbn = libro.getIsbn(); 
    int publicacion = libro.getPublicacion(); 
    double precio = libro.getPrecio(); 
    String descripcion = libro.getDescripcion(); 
    dao.altaLibro(new Libro(ID, titulo, autores, editorial, isbn, publicacion, precio, descripcion)); 

    return false; 
} 

ANA:

case 1: 


     // Crear Editoriales 

     Editorial e1 = new Editorial(1,"Editorial UNO", d1, "A111111"); 
     Editorial e2 = new Editorial(2,"Editorial DOS", d2, "B222222"); 
     Editorial e3 = new Editorial(3,"Editorial TRES", d3, "C333333"); 



     // Crear Libros 

     Libro l1 = new Libro(1, "Libro 1", e1, "1111A", 2001, 11.11, "Este es el libro numero 1"); 
     Libro l2 = new Libro(2, "Libro 2", e1, "2222B", 2002, 22.22, "Este es el libro numero 2"); 
     Libro l3 = new Libro(3, "Libro 3", e1, "3333C", 2003, 33.33, "Este es el libro numero 3"); 
     Libro l4 = new Libro(4, "Libro 4", e2, "4444D", 2004, 44.44, "Este es el libro numero 4"); 
     Libro l5 = new Libro(5, "Libro 5", e2, "5555D", 2005, 55.55, "Este es el libro numero 5"); 
     Libro l6 = new Libro(6, "Libro 6", e3, "6666E", 2006, 66.66, "Este es el libro numero 

SINIF LIBRO:

public class Libro implements Serializable { 


/* 
*   Declaracion de variables 
*/ 


private int ID, publicacion; 
private String titulo, isbn, descripcion; 
private Editorial editorial; 
private Set<Autor> autores = new HashSet<Autor>(); 
private double precio; 


/* 
*   Contructores 
* 
*/ 

public Libro(int ID, String titulo, Set<Autor> autores, Editorial editorial, String isbn, 
     int publicacion, double precio, String descripcion) { 

    this.ID = ID; 
    this.titulo = titulo; 
    this.autores = autores; 
    this.editorial = editorial; 
    this.isbn = isbn; 
    this.publicacion = publicacion; 
    this.precio = precio; 
    this.descripcion = descripcion; 

} 

public Libro(int ID, String titulo, Editorial editorial, String isbn, int publicacion, double precio, String descripcion) { 

    this.ID = ID; 
    this.titulo = titulo; 
    this.editorial = editorial; 
    this.isbn = isbn; 
    this.publicacion = publicacion; 
    this.precio = precio; 
    this.descripcion = descripcion; 

} 

public Libro() 
{ 

} 


/* 
*   Get&Set 
* 
*/ 



public int getID() { 
    return ID; 
} 


public void setID(int iD) { 
    ID = iD; 
} 


public int getPublicacion() { 
    return publicacion; 
} 


public void setPublicacion(int publicacion) { 
    this.publicacion = publicacion; 
} 


public String getTitulo() { 
    return titulo; 
} 


public void setTitulo(String titulo) { 
    this.titulo = titulo; 
} 


public Set<Autor> getAutores() { 
    return autores; 
} 


public void setAutores(Set<Autor> autores) { 
    this.autores = autores; 
} 


public Editorial getEditorial() { 
    return editorial; 
} 


public void setEditorial(Editorial editorial) { 
    this.editorial = editorial; 
} 


public String getIsbn() { 
    return isbn; 
} 


public void setIsbn(String isbn) { 
    this.isbn = isbn; 
} 


public String getDescripcion() { 
    return descripcion; 
} 


public void setDescripcion(String descripcion) { 
    this.descripcion = descripcion; 
} 


public double getPrecio() { 
    return precio; 
} 


public void setPrecio(double precio) { 
    this.precio = precio; 
} 


/* 
* Metodo toString() 
*/ 

public String toString() { 
    return "Libro [ID = " + ID + ", Publicacion = " + publicacion + ", Titulo = " + titulo + ", Autores = " + autores 
      + ", Editorial = " + editorial + ", Isbn = " + isbn + ", Descripcion = " + descripcion + ", Precio = " + precio 
      + "]"; 
} 

/* 
* hashCode() 
*/ 


public int hashCode() { 
    final int prime = 31; 
    int result = 1; 
    result = prime * result + ID; 
    return result; 
} 


/* 
* equals() 
*/ 


public boolean equals(Object obj) { 
    if (this == obj) 
     return true; 
    if (obj == null) 
     return false; 
    if (getClass() != obj.getClass()) 
     return false; 
    Libro other = (Libro) obj; 
    if (ID != other.ID) 
     return false; 
    return true; 
} 


/* 
* Metodos de sincronizaccion 
* 
*/ 

public void addAutor(Autor a){ 
    autores.add(a); 
} 

}

SINIF YAYIN:

public class Editorial { 

private int ID_editorial; 
private String nombre; 
private Direccion direccion; 
private String nif; 
private Set<Libro> libros; 


/* 
* Constructores 
*/ 

public Editorial(int ID_editorial, String nombre, Direccion direccion, String nif, Set<Libro> libros) { 

    this.ID_editorial = ID_editorial; 
    this.nombre = nombre; 
    this.direccion = direccion; 
    this.nif = nif; 
    this.libros = libros; 

} 

public Editorial(int ID_editorial, String nombre, Direccion direccion, String nif) { 

    this.ID_editorial = ID_editorial; 
    this.nombre = nombre; 
    this.direccion = direccion; 
    this.nif = nif; 

} 

public Editorial(){ 

} 


/* 
* Get & Set 
*/ 

public int getID_editorial() { 
    return ID_editorial; 
} 

public void setID_editorial(int ID_editorial) 
{ 
    this.ID_editorial = ID_editorial; 
} 

public String getNombre() { 
    return nombre; 
} 

public void setNombre(String nombre) { 
    this.nombre = nombre; 
} 

public Direccion getDireccion() { 
    return direccion; 
} 

public void setDireccion(Direccion direccion) { 
    this.direccion = direccion; 
} 

public String getNif() { 
    return nif; 
} 

public void setNif(String nif) { 
    this.nif = nif; 
} 

public Set<Libro> getLibros() { 
    return libros; 
} 

public void setLibros(Set<Libro> libros) { 
    this.libros = libros; 
} 


/* 
* Metodo toString() 
*/ 



public String toString() { 
    return "Editorial [Id Editorial=" + ID_editorial + ", nombre=" + nombre + ", direccion=" + direccion + ", nif=" + nif + ", libros=" + libros + "]"; 
} 

cevap

0

Kişisel oturum yönetimi yanlış, içine Sınıf LibrosDAO değiştirin:

public class LibrosDAO implements ItfzLibrosDao { 

SessionFactory sf = new Configuration().configure("hibernate2.cfg.xml").buildSessionFactory(); 

Session session; 

Transaction tx; 

/* 
* Crea un nuevo registro en la tabla con los datos del libro recibido como 
* argumento 
*/ 

public boolean altaLibro(Libro libro) { 
    // Creo una variable de tipo boolean que me retorna si se ha podido o no 
    // insertar un nuevo libro 
    session = sf.openSession(); 
    boolean insertado = false; 

    Libro l = new Libro(libro.getID(),libro.getTitulo(),libro.getAutores(),libro.getEditorial(),libro.getIsbn(), 
      libro.getPublicacion(),libro.getPrecio(),libro.getDescripcion()); 

    try { 
     tx = session.beginTransaction(); 

     session.save(l); 
     tx.commit(); 
     insertado= true;  
     } catch(Exception ex) { 
      tx.rollback(); 
      ex.printStackTrace(); 
      insertado = false; 
     } finally { 
      session.close(); 

     } 

     return insertado; 
} 
İlgili konular