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初學者。有人可以解釋我在這裏發生了什麼問題嗎?
您運行的是哪個版本的Java? – Thomas 2012-02-15 21:21:37
這不是Hibernate問題,您的JVM版本是否在5之前? Hibernate 4不支持JVM和5之前的版本,但我不確定,但也許Hibernate 4只適用於JVM 6+ – 2012-02-15 21:27:42