Java 初始SessionFactory创建失败:org.hibernate.MappingException:配置无效

Java 初始SessionFactory创建失败:org.hibernate.MappingException:配置无效,java,mysql,hibernate,Java,Mysql,Hibernate,已解决 这个问题是关于java hibernate的 当我运行主类时,我得到了这个错误 Initial SessionFactory creation failed: org.hibernate.MappingException: invalid configuration Exception in thread "main" java.lang.NullPointerException at principal.ClienteDAO.guardaCliente(ClienteDAO.java

已解决

这个问题是关于java hibernate的

当我运行主类时,我得到了这个错误

Initial SessionFactory creation failed: org.hibernate.MappingException: 
invalid configuration
Exception in thread "main" java.lang.NullPointerException
at principal.ClienteDAO.guardaCliente(ClienteDAO.java:38)
at principal.Main.main(Main.java:31)
C:\Users\Nico\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)
我不知道为什么我会得到那个例外

我的hibernate.cfg.xml是

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <!--TODA LA INFORMACION FUE SACADA DE: http://www.javatutoriales.com/2009/05/hibernate-parte-1-persistiendo-objetos.html-->

    <!-- parametros para la conexion a la base de datos -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/basededatosprueba</property>
    <property name="connection.username">root</property>
    <property name="connection.password"></property>

    <!-- Configuracion del pool interno -->
    <property name="connection.pool_size">1</property>

    <!-- Dialecto de la base de datos -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- Otras propiedades importantes -->
    <property name="show_sql">true</property> 
    <property name="hbm2ddl.auto">create-drop</property>

    <!-- Archivos de mapeo -->
    <mapping resource="mapeos/Cliente.hbm.xml"/>
</session-factory>
</hibernate-configuration>
我的课堂是

public class ClienteDAO {  
private Session sesion; 
private Transaction tx;  

public int guardaCliente(Cliente cliente) throws HibernateException 
{ 
    int id = 0;  

    try 
    { 
        iniciaOperacion(); 
        id = (int) sesion.save(cliente); 
        tx.commit(); 
    } catch (HibernateException he) 
    { 
        manejaExcepcion(he); 
        throw he; 
    } finally 
    { 
        sesion.close(); 
    }  

    return id; 
}  

public void actualizaCliente(Cliente cliente) throws HibernateException 
{ 
    try 
    { 
        iniciaOperacion(); 
        sesion.update(cliente); 
        tx.commit(); 
    } catch (HibernateException he) 
    { 
        manejaExcepcion(he); 
        throw he; 
    } finally 
    { 
        sesion.close(); 
    } 
}  

public void eliminaCliente (Cliente cliente) throws HibernateException 
{ 
    try 
    { 
        iniciaOperacion(); 
        sesion.delete(cliente); 
        tx.commit(); 
    } catch (HibernateException he) 
    { 
        manejaExcepcion(he); 
        throw he; 
    } finally 
    { 
        sesion.close(); 
    } 
}  

public Cliente obtenContacto(int codCliente) throws HibernateException 
{ 
   Cliente cliente = null;  
    try 
    { 
        iniciaOperacion(); 
        cliente = (Cliente) sesion.get(Cliente.class, codCliente); 
    } finally 
    { 
        sesion.close(); 
    }  

    return cliente; 
}  

public List<Cliente> obtenListaContactos() throws HibernateException 
{ 
    List<Cliente> listaContactos = null;  

    try 
    { 
        iniciaOperacion(); 
        listaContactos = sesion.createQuery("from Contacto").list(); 
    } finally 
    { 
        sesion.close(); 
    }  

    return listaContactos; 
}  

private void iniciaOperacion() throws HibernateException 
{ 
    sesion = HibernateUtil.getSessionFactory().openSession(); 
    tx = sesion.beginTransaction(); 
}  

private void manejaExcepcion(HibernateException he) throws HibernateException 
{ 
    tx.rollback(); 
    throw new HibernateException("Ocurrió un error en la capa de acceso a datos", he); 
} 
}
公共类客户端{
非公开会议;
私人交易;
public int-guardaclient(Cliente-Cliente)抛出HibernateException
{ 
int id=0;
尝试
{ 
iniciaOperacion();
id=(int)sesion.save(客户);
tx.commit();
}捕获(冬眠异常he)
{ 
曼内贾普西翁(he);
扔他;
}最后
{ 
sesion.close();
}  
返回id;
}  
public void actualizaCliente(Cliente Cliente)抛出hibernateeexception
{ 
尝试
{ 
iniciaOperacion();
更新(客户);
tx.commit();
}捕获(冬眠异常he)
{ 
曼内贾普西翁(he);
扔他;
}最后
{ 
sesion.close();
} 
}  
public void eliminaCliente(Cliente Cliente)抛出hibernateeexception
{ 
尝试
{ 
iniciaOperacion();
删除(客户);
tx.commit();
}捕获(冬眠异常he)
{ 
曼内贾普西翁(he);
扔他;
}最后
{ 
sesion.close();
} 
}  
公共客户机obtenContacto(int-codCliente)抛出HibernateException
{ 
Cliente Cliente=null;
尝试
{ 
iniciaOperacion();
cliente=(cliente)sesion.get(cliente.class,codCliente);
}最后
{ 
sesion.close();
}  
回头客;
}  
public List obtEnstractaContactOS()引发HibernateException
{ 
List listaContactos=null;
尝试
{ 
iniciaOperacion();
listaContactos=sesion.createQuery(“来自Contacto”).list();
}最后
{ 
sesion.close();
}  
返回listaContactos;
}  
IniciaOperation()中的私有void引发HibernateException
{ 
sesion=HibernateUtil.getSessionFactory().openSession();
tx=sesion.beginTransaction();
}  
私有void manejaexception(hibernateeexception he)抛出hibernateeexception
{ 
tx.回滚();
抛出新的HibernateeException(“他说,在接受数据的过程中出现了错误”);
} 
}
我想跑

public static void main(String[] args) {

    int idAEliminar = 0;
    ClienteDAO clienteDAO = new ClienteDAO(); 
    Cliente contactoRecuperado;

    //Creamos tes instancias de Contacto  (String nombre, String direccion, int codigoPostal, String telefono, String cuit)
    Cliente cliente1 = new Cliente("Pedro", "dark 340", 3080, "86484","15531"); 
    Cliente cliente2 = new Cliente("Manuel", "orark 780", 5160, "86484","15531"); 
    Cliente cliente3 = new Cliente("Martin", "docrk 495", 3060, "86484","15531");  

    //Guardamos las tres instancias, guardamos el id del contacto1 para usarlo posteriormente 
    idAEliminar = clienteDAO.guardaCliente(cliente1); 
    clienteDAO.guardaCliente(cliente2);
    clienteDAO.guardaCliente(cliente3);

    //Modificamos el contacto 2 y lo actualizamos 
    cliente2.setNombre("Nuevo Contacto 2");
    clienteDAO.actualizaCliente(cliente2);

    //Recuperamos el contacto1 de la base de datos 
    contactoRecuperado = clienteDAO.obtenContacto(idAEliminar);
    System.out.println("Recuperamos a " + contactoRecuperado.getNombre());

    //Eliminamos al contactoRecuperado (que es el contacto3)
    clienteDAO.eliminaCliente(contactoRecuperado);

    //Obtenemos la lista de contactos que quedan en la base de datos y la mostramos
    List<Cliente> listaContactos = clienteDAO.obtenListaContactos();
    System.out.println("Hay " + listaContactos.size() + " clientes en la base de datos");

    for(Cliente c : listaContactos)
    {System.out.println("-> " + c.getNombre());
    } 
}
publicstaticvoidmain(字符串[]args){
int-idAEliminar=0;
ClienteDAO ClienteDAO=新ClienteDAO();
客户联系人或客户联系人;
//联系方式(字符串名称、字符串指示、内部编码、字符串电话、字符串提示)
客户1=新客户(“佩德罗”、“黑暗340”、“3080”、“86484”、“15531”);
客户客户2=新客户(“Manuel”、“orark 780”、“5160”、“86484”、“15531”);
客户3=新客户(“马丁”、“Dock495”、“3060”、“86484”、“15531”);
//一个实例的担保人,一个事后担保人
idAEliminar=clienteDAO.guardaclient(client1);
客户:GuardClient(客户2);
客户:GuardClient(客户3);
//2号联系人和2号联系人的修改
客户2.setNombre(“新联系人2”);
实施客户(客户2);
//达托斯基地的一个接触点
contactoRecuperado=clienteDAO.obtenContacto(idAEliminar);
System.out.println(“Recurperamos a”+contactoRecuperado.getNombre());
//Eliminamos al contactoRecuperado(请参阅contacto3)
clienteDAO.eliminaCliente(contactoRecuperado);
//在datos和la mostramos的基础上建立联系
List listaContactos=clienteDAO.obtenListaContactos();
System.out.println(“Hay”+listaContactos.size()+“客户基本数据”);
针对(客户c:listaContactos)
{System.out.println(“->”+c.getNombre());
} 
}
编辑:

my Class.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="principal.Cliente" table="CLIENTE">
    <id name="codigo" column="codigo">
        <generator class="identity" />
    </id>
    <property name="nombre" type="string" column="nombre"/>
    <property name="direccion" type="string" column="direccion"/>
    <property name="telefono" type="string" column="telefono"/>
    <property name="cuit" type="string" column="cuit"/>
    <property name="codigoPostal" type="int" column="cp"/>
    <property name="saldo" type="double" column="saldo"/>
    <property name="deuda" type="double"  column="deuda"/> 
</class>
</hibernate-mapping>

编辑2: 所以,我不知道为什么,但是我更改了hibernate.cfg.xml,现在它可以工作了,就在这里

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/basededatosprueba</property>
    <property name="connection.username">root</property>
    <property name="connection.password"></property>
    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>
    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>
    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>
    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>

    <mapping resource="mapeos/Cliente.hbm.xml"/>
</session-factory>
</hibernate-configuration>

com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/basededatosprueba
根
1.
org.hibernate.dialogue.mysql5dialogue
线
org.hibernate.cache.NoCacheProvider
真的
创造

解决了?我们将看到

我认为您可能需要关闭xml中的hibernate配置标记。

是的,我没有注意到代码中没有显示它,谢谢,前提是异常是
org.hibernate.MappingException
,如果您可以共享
Cliente.hbm.xml
和pojook,我编辑了它,就是这样:)
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/basededatosprueba</property>
    <property name="connection.username">root</property>
    <property name="connection.password"></property>
    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>
    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>
    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>
    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>

    <mapping resource="mapeos/Cliente.hbm.xml"/>
</session-factory>
</hibernate-configuration>