有人知道为什么会一直这样吗? 我正在使用Hibernate,Mysql,Maven,Spring。 我在查看每个帖子但我无法找到解决方案。 谢谢:))
Class Libro
package com.webservice.serviceDos;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "libros")
public class Libro implements java.io.Serializable
{
private int libroIsbn;
private String libroAutor;
private String libroTitulo;
private String libroTema;
public Libro()
{}
public Libro(int isbn, String autor, String titulo,String tema)
{
this.libroIsbn=isbn;
this.libroAutor=autor;
this.libroTitulo=titulo;
this.libroTema=tema;
}
@Id
@Column(name = "LIBRO_ISBN", unique = true, nullable = false, precision = 5, scale = 0)
public int getLibroIsbn()
{
return this.libroIsbn;
}
public void setLibroIsbn(int isbn)
{
this.libroIsbn=isbn;
}
@Column(name = "LIBRO_AUTOR", nullable = false, length = 50)
public String getLibroAutor()
{
return this.libroAutor;
}
public void setLibroAutor(String autor) {
this.libroAutor = autor;
}
@Column(name = "LIBRO_TITULO", nullable = false, length = 50)
public String getLibroTitulo()
{
return this.libroTitulo;
}
public void setLibroTitulo(String titulo) {
this.libroTitulo = titulo;
}
@Column(name = "LIBRO_TEMA", nullable = false, length = 50)
public String getLibroTema()
{
return this.libroTema;
}
public void setLibroTema(String tema) {
this.libroTema = tema;
}
// public String toString() {
// return "Isbn: '" + this.libroIsbn + "' Autor: '" + this.libroAutor + "' Titulo: '" + this.libroTitulo + " 'Tema: '" + this.libroTema + "'";
// }
}
Class LibroDaoInterface
package com.webservice.serviceDos;
import java.io.Serializable;
import java.util.List;
public interface LibroDaoInterface<T, Id extends Serializable>
{
public void persist(T entity);
public T findById(int id);
public void delete(T entity);
public List<T> findAll();
public void deleteAll();
}
LibroDao
package com.webservice.serviceDos;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import com.webservice.serviceDos.Libro;
public class LibroDao implements LibroDaoInterface<Libro, String>
{
private Session currentSession;
private Transaction currentTransaction;
public LibroDao(){}
public Session openCurrentSession()
{
currentSession = getSessionFactory().openSession();
return currentSession;
}
public Session OpenCurrentSessionwithTransaction()
{
currentSession = getSessionFactory().openSession();
currentTransaction = currentSession.beginTransaction();
return currentSession;
}
public void closeCurrentSession()
{
currentSession.close();
}
public void closeCurrentSessionwithTransaction()
{
currentTransaction.commit();
currentSession.close();
}
public static SessionFactory getSessionFactory()
{
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
return sessionFactory;
}
public Session getCurrentSession()
{
return currentSession;
}
public void setCurrentSession(Session currentSession)
{
this.currentSession = currentSession;
}
public Transaction getSessionTransaction()
{
return currentTransaction;
}
public void setCurrentTransaction(Transaction currentTransaction)
{
this.currentTransaction=currentTransaction;
}
public void persist(Libro entity)
{
getCurrentSession().save(entity);
}
public Libro findById(int id)
{
Libro libro = (Libro)getCurrentSession().get(Libro.class, id);
return libro;
}
public void delete(Libro entity)
{
getCurrentSession().delete(entity);
}
@SuppressWarnings("unchecked")
public List<Libro> findAll()
{
List<Libro> libros = (List<Libro>)getCurrentSession().createQuery("from libros").list();
return libros;
}
public void deleteAll()
{
List<Libro> entityList = findAll();
for(Libro entity : entityList)
{
delete(entity);
}
}
}
LibroService
package com.webservice.serviceDos;
import java.util.List;
import com.webservice.serviceDos.Libro;
import com.webservice.serviceDos.LibroDao;
import org.hibernate.exception.JDBCConnectionException;
public class LibroService
{
public static LibroDao libroDao;
public LibroService()
{
libroDao = new LibroDao();
}
public void persist(Libro entity)
{
try
{
libroDao.OpenCurrentSessionwithTransaction();
libroDao.persist(entity);
libroDao.closeCurrentSessionwithTransaction();
}
catch(JDBCConnectionException e)
{
System.out.println(e);
}
}
public Libro findById(int id)
{
libroDao.openCurrentSession();
Libro libro = libroDao.findById(id);
libroDao.closeCurrentSession();
return libro;
}
public void delete(int id)
{
libroDao.OpenCurrentSessionwithTransaction();
Libro libro = libroDao.findById(id);
libroDao.delete(libro);
libroDao.closeCurrentSessionwithTransaction();
}
public List<Libro> findAll()
{
libroDao.openCurrentSession();
List<Libro> libros = libroDao.findAll();
libroDao.closeCurrentSession();
return libros;
}
public void deleteAll()
{
libroDao.OpenCurrentSessionwithTransaction();
libroDao.deleteAll();
libroDao.closeCurrentSessionwithTransaction();
}
public LibroDao libroDao()
{
return libroDao;
}
}
的hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/biblioteca</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.hmb2dll.auto">update</property>
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="com.webservice.serviceDos.Libro"/>
</session-factory>
</hibernate-configuration>
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.webservice</groupId>
<artifactId>serviceDos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>serviceDos</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
</project>
错误
ago 28, 2016 4:59:16 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
ago 28, 2016 4:59:16 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
ago 28, 2016 4:59:16 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
ago 28, 2016 4:59:16 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
ago 28, 2016 4:59:16 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
ago 28, 2016 4:59:16 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
ago 28, 2016 4:59:16 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
ago 28, 2016 4:59:16 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
ago 28, 2016 4:59:16 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/biblioteca]
ago 28, 2016 4:59:16 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
ago 28, 2016 4:59:16 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
ago 28, 2016 4:59:16 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
ago 28, 2016 4:59:17 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
ago 28, 2016 4:59:17 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
ago 28, 2016 4:59:17 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
ago 28, 2016 4:59:17 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:185)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:135)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:401)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
at com.webservice.serviceDos.LibroDao.getSessionFactory(LibroDao.java:49)
at com.webservice.serviceDos.LibroDao.OpenCurrentSessionwithTransaction(LibroDao.java:29)
at com.webservice.serviceDos.LibroService.persist(LibroService.java:23)
at com.webservice.serviceDos.App.main(App.java:14)
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:138)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:188)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:403)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:520)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:148)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:163)
... 7 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:135)
... 16 more
Caused by: java.lang.NoClassDefFoundError: javassist/util/proxy/MethodFilter
at org.hibernate.bytecode.internal.javassist.BytecodeProviderImpl.getProxyFactoryFactory(BytecodeProviderImpl.java:58)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactoryInternal(PojoEntityTuplizer.java:244)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:222)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:212)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:80)
... 21 more
Caused by: java.lang.ClassNotFoundException: javassist.util.proxy.MethodFilter
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 26 more
答案 0 :(得分:3)
引起:java.lang.NoClassDefFoundError:javassist / util / proxy / MethodFilter
您需要在项目中加入javassist。添加到pom.xml:
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>