hibernate 4.3
使用hibernate4.3的時候獲取SessionFactory和之前不一樣,網上看到一篇(StackOverFlow裡面的),可以使用下面的方式:
package util.hibernate; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static SessionFactory factory; static{ try { Configuration configuration = new Configuration().configure(); StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); factory = configuration.configure().buildSessionFactory(serviceRegistry); } catch (HibernateException e) { e.printStackTrace(); } } public static SessionFactory getSessionFactory(){ return factory; } public static Session getOpenSession(){ return factory.openSession(); } public static Session getCurrentSession(){ return factory.getCurrentSession(); } public static void closeSession(Session session){ if(session!=null){ if(session.isOpen()){ session.close(); } } } }
使用StandardServiceRegister類來初始化我們的SessionFactory。
查看本欄目
但是在使用注解的時候,卻出現了下面的錯誤:
java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index; at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:936) at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:781) at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3762) at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3716) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) at util.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:17) at HibernateTest.testSave(HibernateTest.java:24) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41) at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.junit.runners.ParentRunner.run(ParentRunner.java:220) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
結果搞了好久都沒搞定,但是我之前用4.3 的是可以的,怎麼回事?
通過對比,我發現在model類中如果使用了Table(name="t_user"),就會報這樣的錯誤,但是如果我寫成@Entity(name="t_user")這樣就沒問題了,這個可能是Hibernate4.3的一個bug?這就不得而知了,不過如果用4.3 的話,那麼直接使用Entity來指定表名就可以解決這個問題了。