Hibernate3.3以上版本报Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/Cacheable异常

时间:2023-01-19 23:35:11

Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/Cacheable

我用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.6 annotation3.4
三个java类
Teacher.java

package com.unis.hibernate.model;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table
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;
}

}

package com.unis.hibernate.main;

import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import com.unis.hibernate.model.Teacher;
import com.unis.hibernate.util.HibernateUtil;

public class TeacherMain {

/**
* @param args
*/
public static void main(String[] args) {
TeacherMain stu = new TeacherMain();
Teacher teacher = new Teacher();
teacher.setName("likai");
teacher.setTitle("Hibernate");
stu.addStudent(teacher);
}

public void addStudent(Teacher teacher){
Session session = null;
Transaction ts = null;
try{
session = HibernateUtil.getSessionFactory().openSession();
ts = session.beginTransaction();
session.save(teacher);
ts.commit();

}finally{
if(session != null){
session.close();}
}
}
}

package com.unis.hibernate.util;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;


public final class HibernateUtil {

private static SessionFactory sfc;

private HibernateUtil(){

}
static{
Configuration cfg = new AnnotationConfiguration();
cfg.configure();
sfc = cfg.buildSessionFactory();
}

public static SessionFactory getSessionFactory(){
return sfc;
}
public static Session getSession(){
return sfc.openSession();
}

}

hibernate.cfg.xml中的mapping
<mapping class="com.unis.hibernate.model.Teacher"/>
 
推荐答案
javax.persistence.Cacheable 是 JPA 2.0 规范中的东西!

需要加入hibernate-distribution-3.6.0-Final/lib/jpa目录下的hibernate-jpa-2.0-api-1.0.0.Final.jar加到web工程的lib包下