public class HibernateTest {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
public static void main(String ars[]){
UserDetails user = new UserDetails();
user.setUserId(1);
user.setUserName("First user");
SessionFactory sessionFactory = configureSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}
//using Hibernate API
private static SessionFactory configureSessionFactory() throws HibernateException {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
}
错误:
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at hibernate.HibernateTest.configureSessionFactory(HibernateTest.java:30)
at hibernate.HibernateTest.main(HibernateTest.java:20)
我是Hibernate的初学者。谁能解释一下这里出了什么问题?
答案 0 :(得分:0)
此:
线程“main”中的异常java.lang.UnsupportedClassVersionError:.class中的错误版本号
基本上意味着您使用的Java版本对于您尝试运行的代码来说太旧了。
您使用的是哪个版本的Java?
答案 1 :(得分:0)
是java.lang.UnsupportedClassVersionError: Bad version number in .class file
您的运行时(jvm)不支持hibernate jar的错误版本。
答案 2 :(得分:0)
使用以下命令
检查hibernate类的JDK版本javap -verbose org.hibernate.SessionFactory.SessionFactory
(首先解开jar)
确保您的HibernateTest也在同一版本中编译。