java - HIBERNATE4 org.hibernate.MappingException: Unknown entity -


i'm learnig hibernate4 , have encountered problem while trying xml hibernate mapping.

i'm using eclipse , mysql.

the error when try insert object in db table: org.hibernate.mappingexception: unknown entity: com.hibernate.gestionproductos.modelo.proveedores

hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8"?> <!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd 3.0//en"                                          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>  <session-factory name="sfbdhibernate">   <property name="hibernate.connection.driver_class">com.mysql.jdbc.driver</property>   <property name="hibernate.connection.password">root</property>   <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bdhibernate</property>   <property name="hibernate.connection.username">root</property>   <property name="hibernate.dialect">org.hibernate.dialect.mysqldialect</property>   <property name="hibernate.show_sql">true</property>   <property name="hibernate.current_session_context_class">thread</property>   <mapping class="com.hibernate.gestionproductos.modelo.proveedores"/>  </session-factory> </hibernate-configuration> 

you can see class proveedores mapped.

proveedoresdao.java:

package com.hibernate.gestionproductos.dao;  import java.util.list;  import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.boot.registry.standardserviceregistrybuilder; import org.hibernate.cfg.configuration;  import com.hibernate.gestionproductos.modelo.proveedores;  public class proveedoresdao {      private sessionfactory getsessionfactory() { configure hay que ponerle el path         configuration configuracion = new configuration().configure();         // hibernate 4         standardserviceregistrybuilder builder = new standardserviceregistrybuilder()                                                     .applysettings(configuracion.getproperties());         sessionfactory sf = configuracion.buildsessionfactory(builder.build());         return sf;       }      public void insertarproveedor(proveedores prov) {         try {             sessionfactory sf = getsessionfactory();             session sesion = sf.opensession();             org.hibernate.transaction tx = sesion.begintransaction();             sesion.save(prov);              tx.commit();             sesion.close();         } catch (exception e) {             // todo auto-generated catch block             e.printstacktrace();         }        }      public list<proveedores> listarproveedores() {         try {             sessionfactory sf = getsessionfactory();             session sesion = sf.opensession();             org.hibernate.transaction tx = sesion.begintransaction();              list<proveedores> lista = sesion.createcriteria(proveedores.class).list();             tx.commit();             sesion.close();             return lista;         } catch (exception e) {             // todo auto-generated catch block             e.printstacktrace();             return null;         }        } } 

proveedores.java:

package com.hibernate.gestionproductos.modelo;  public class proveedores implements java.io.serializable {      private long idproveedores;     private string nombre;     private string contacto;     private string email;     private string telefono;      public proveedores() {     }      public proveedores(string nombre, string email) {         this.nombre = nombre;         this.email = email;     }      public proveedores(string nombre, string contacto, string email, string telefono) {         this.nombre = nombre;         this.contacto = contacto;         this.email = email;         this.telefono = telefono;     }      public long getidproveedores() {         return this.idproveedores;     }      public void setidproveedores(long idproveedores) {         this.idproveedores = idproveedores;     }      public string getnombre() {         return this.nombre;     }      public void setnombre(string nombre) {         this.nombre = nombre;     }      public string getcontacto() {         return this.contacto;     }      public void setcontacto(string contacto) {         this.contacto = contacto;     }      public string getemail() {         return this.email;     }      public void setemail(string email) {         this.email = email;     }      public string gettelefono() {         return this.telefono;     }      public void settelefono(string telefono) {         this.telefono = telefono;     }  } 

proveedores.hbm.xml:

<?xml version="1.0"?> <!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  <hibernate-mapping>     <class name="com.hibernate.gestionproductos.modelo.proveedores" table="proveedores" catalog="bdhibernate">         <id name="idproveedores" type="java.lang.long">             <column name="idproveedores" />             <generator class="identity" />         </id>         <property name="nombre" type="string">             <column name="nombre" length="45" not-null="true" />         </property>         <property name="contacto" type="string">             <column name="contacto" length="45" />         </property>         <property name="email" type="string">             <column name="email" length="100" not-null="true" />         </property>         <property name="telefono" type="string">             <column name="telefono" length="12" />         </property>     </class> </hibernate-mapping> 

configuracionxml.java:

package com.hibernate.gestionproductos.programa;  import java.util.list;  import com.hibernate.gestionproductos.dao.proveedoresdao; import com.hibernate.gestionproductos.modelo.proveedores;  public class configuracionxml {      public static void main(string[] args) {         proveedores prov = new proveedores();         prov.setnombre("proveedor 3");         prov.setcontacto("juan");         prov.setemail("juan@proveedor3.com");         prov.settelefono("632227612");          proveedoresdao dao = new proveedoresdao();         dao.insertarproveedor(prov);         system.out.println("se ha insertado el proveedor");          list<proveedores> proveedores = dao.listarproveedores();         system.out.println("listado de proveedores:\n");         (proveedores p : proveedores) {             system.out.println(p.getidproveedores() + " - " + p.getnombre());         }     }  } 

here @ dao.insertarproveedor(prov) breaks:

org.hibernate.mappingexception: unknown entity: com.hibernate.gestionproductos.modelo.proveedores se ha insertado el proveedor @ org.hibernate.internal.sessionfactoryimpl.getentitypersister(sessionfactoryimpl.java:776) @ org.hibernate.internal.sessionimpl.getentitypersister(sessionimpl.java:1451) .....

anyone can help?

the issue hibernate.cfg.xml have mentioned

<mapping class="com.hibernate.gestionproductos.modelo.proveedores"/> 

this tells hibernate system read class entity, class doesn't has such information. above configuration(or class property of mapping tag) used when use annotations describe entity. in case using hbm xml provide info entity, use resource property of mapping tag in following way -

<mapping resource="proveedores.hbm.xml"/> 

so replacing line in hbm xml doc make hibernate read mapping definitions provided. didn't specify now, mapping info not being read, , hibernate doesn't know entity resulting in unknown entity.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -