问题描述
先帖代码吧HibernateUtils.javapackage com;import org.apache.log4j.Logger; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.Session; public class HibernateUtils {private static final Logger logger = Logger.getLogger(HibernateUtils.class);private static final SessionFactory sessionFactory;static {try {//System.out.println(new AnnotationConfiguration().configure().buildSessionFactory());sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();//sessionFactory = new AnnotationConfiguration().configure("/hibernate.cfg.xml").buildSessionFactory(); } catch (Exception e) {logger.error(e.getMessage());throw new ExceptionInInitializerError(e);}}public static Session getSession() {return sessionFactory.openSession();}}User.javapackage com;import javax.persistence.*;@Entity@Table(name="user") public class User {@Id@GeneratedValue(strategy = GenerationType.AUTO)private Integer id;private String name;private String password;private String email;public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}MainClass.javapackage com;import org.hibernate.Session;import org.hibernate.Transaction;public class MainClass {/** * @param args */public static void main(String[] args) {User user = new User();user.setName("andy");user.setPassword("123456");user.setEmail("andy@123.com");Session session = HibernateUtils.getSession();Transaction tx = session.beginTransaction();session.saveOrUpdate(user);tx.commit();}}执行的时候出现Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/ReflectionManagerat com.HibernateUtils.<clinit>(HibernateUtils.java:17)at com.MainClass.main(MainClass.java:17)错误,不知道怎么解决.第一个demo希望支持下
解决方案
差一个jar包, 将hibernate-commons-annotations.jar加入到classpath中