我用annotations做hibernate操作时报这个错误请问是什么原因:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/Cacheable
at org.hibernate.cfg.AnnotationBinder.determineCacheSettings(AnnotationBinder.java:953)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:596)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:636)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:359)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1377)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
at TeacherTest.main(TeacherTest.java:17)
环境,hibernate3.5.5_annotations3.4.0
两个java类
TeacherTest
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import com.bjsxt.hibernate.model.Teacher;
public class TeacherTest {
public static void main(String[] args){
Teacher t=new Teacher();
t.setId(1);
t.setName("t1");
t.setTitle("教受");
Configuration cfg=new AnnotationConfiguration();
SessionFactory sf=cfg.configure().buildSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
sf.close();
}
}
Teacher
package com.bjsxt.hibernate.model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Teacher {
private int id;
private String name;
private String title;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
hibernate.cfg.xml中的mapping
<mapping class="com.bjsxt.hibernate.model.Teacher"/>
解决办法:
javax.persistence.Cacheable 是 JPA 2.0 规范中的东西!
需要加入hibernate-distribution-3.5.0-Final\lib\jpa目录下的hibernate-jpa-2.0-api-1.0.0.Final.jar
到path中
步骤:window->preferences-java->bulid path-userliberlay-hibernate-->add JARs->选择你的hibernate-jpa-2.0-api-1.0.0.Final.jar目录名,保存重新编译,问题解决