0
我在運行示例時遇到了一些問題。休眠和MySQL。配置休眠時出錯5.2.10
我跟着一個教程:http://www.javatutoriales.com/2009/05/hibernate-parte-2-persistiendo-objetos.html
而且我改變了一些線條閱讀本Foro的。
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">
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url"> jdbc:mysql://localhost:3306/base_prueba</property>
<property name="connection.username">root</property>
<property name="connection.password">tomazon-33</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create-drop</property>
<mapping class="mapeo/Contacto.hbm.xml"/>
</session-factory>
</hibernate-configuration>
主要:
package Main;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.transaction.HeuristicMixedException;
import javax.transaction.RollbackException;
import org.hibernate.HibernateException;
public class Main
{
public static void main(String[] args) throws HibernateException
{
try {
ContactosDAO contactosDAO = new ContactosDAO();
Contacto contactoRecuperado = null;
long idAEliminar = 0;
Contacto contacto1 = new Contacto("Contacto 1", "[email protected]", "12345678");
Contacto contacto2 = new Contacto("Contacto 2", "[email protected]", "87654321");
Contacto contacto3 = new Contacto("Contacto 3", "[email protected]", "45612378");
idAEliminar = contactosDAO.guardaContacto(contacto1);
contactosDAO.guardaContacto(contacto2);
contactosDAO.guardaContacto(contacto3);
contacto2.setNombre("Nuevo Contacto 2");
contactosDAO.actualizaContacto(contacto2);
contactoRecuperado = contactosDAO.obtenContacto(idAEliminar);
System.out.println("Recuperamos a " + contactoRecuperado.getNombre());
contactosDAO.eliminaContacto(contactoRecuperado);
List<Contacto> listaContactos = contactosDAO.obtenListaContactos();
System.out.println("Hay " + listaContactos.size() + "contactos en la base de datos");
for(Contacto c : listaContactos)
{
System.out.println("-> " + c.getNombre());
}
} catch (RollbackException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (HeuristicMixedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Contactos:
package Main;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="contactos")
public class Contacto implements Serializable
{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
private String nombre;
@Column(name="e_mail")
private String email;
private String telefono;
public Contacto()
{
}
public Contacto(String nombre, String email, String telefono)
{
this.nombre = nombre;
this.email = email;
this.telefono = telefono;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public long getId()
{
return id;
}
private void setId(long id)
{
this.id = id;
}
public String getNombre()
{
return nombre;
}
public void setNombre(String nombre)
{
this.nombre = nombre;
}
public String getTelefono()
{
return telefono;
}
public void setTelefono(String telefono)
{
this.telefono = telefono;
}
}
Contactos-DAO:
public class ContactosDAO
{
private Session sesion;
private Transaction tx;
public long guardaContacto(Contacto contacto) throws HibernateException, RollbackException, HeuristicMixedException
{
long id = 0;
try
{
iniciaOperacion();
id = (Long) sesion.save(contacto);
tx.commit();
} catch (HibernateException he)
{
try {
manejaExcepcion(he);
throw he;
} catch (IllegalStateException | SystemException ex) {
Logger.getLogger(ContactosDAO.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (HeuristicRollbackException | SecurityException | SystemException ex) {
Logger.getLogger(ContactosDAO.class.getName()).log(Level.SEVERE, null, ex);
} finally
{
sesion.close();
}
return id;
}
的HibernateUtil:
package hibernatenormal;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil
{
private static final SessionFactory sessionFactory;
static
{
try
{
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException he)
{
System.err.println("Ocurrió un error en la inicialización de la SessionFactory: " + he);
throw new ExceptionInInitializerError(he);
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
而且錯誤:
run:
jul 24, 2017 8:03:07 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.10.Final}
jul 24, 2017 8:03:07 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
jul 24, 2017 8:03:07 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
Ocurrió un error en la inicialización de la SessionFactory: org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number -1 and column -1 in RESOURCE hibernate.cfg.xml. Message: cvc-elt.1: No se ha encontrado la declaración del elemento 'dependency'.
Exception in thread "main" java.lang.NullPointerException
at Main.ContactosDAO.guardaContacto(ContactosDAO.java:59)
at Main.Main.main(Main.java:34)
C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)