我是hibernate的新手,我正在尝试测试我的实体(持久性) 并且此错误一直显示
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/boot/registry/classloading/spi/ClassLoaderService
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:80)
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:71)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:52)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
at Pers.PersistTest.main(PersistTest.java:20)
Caused by: java.lang.ClassNotFoundException:org.hibernate.boot.registry.classloading.spi.ClassLoaderService
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
这是我的实体
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int idUser;
...
private boolean isAdmin;
(当然使用getter和setter以及构造函数)
这是测试类
public class PersistTest {
public static void main(String[] args) {
EntityManagerFactory emf=Persistence.createEntityManagerFactory("PU");
EntityManager em = emf.createEntityManager();
Session s = new AnnotationConfiguration()
.configure().buildSessionFactory().openSession();
Transaction t = s.beginTransaction();
User c = new User();
c.setIdUser(1);
...
c.setAdmin(true);
em.persist(c);
t.commit();
s.close();
System.out.print("success");
}
这是persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/GBO1" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
</properties>
这是我的hibernate依赖
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>ejb3-persistence</artifactId>
<version>3.3.2.Beta1</version>
</dependency>
并提前感谢:)
答案 0 :(得分:5)
EntityMananger
)和Hibernate Session
方法。hibernate-entitymanager
(5.0.6.Final)。 您可以使用入门