【发布时间】:2015-11-01 17:07:57
【问题描述】:
我正在学习 Hibernate4,在尝试 XML hibernate 映射时遇到了这个问题。
我正在使用 eclipse 和 MySQL。
当我尝试在数据库表中插入我的对象时的错误: org.hibernate.MappingException:未知实体: 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>
您可以看到 Proveedores 类已被映射。
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");
for (Proveedores p : proveedores) {
System.out.println(p.getIdproveedores() + " - " + p.getNombre());
}
}
}
在 dao.insertarProveedor(prov) 中它会中断:
org.hibernate.MappingException:未知实体: com.hibernate.gestionproductos.modelo.Proveedores Se ha insertado el 证明人在 org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776) 在 org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1451) .....
有人可以帮忙吗?
【问题讨论】:
标签: java hibernate hibernate-mapping