hibernate 4.3
使用hibernate4.3的时候获取SessionFactory和之前不一样,网上看到一篇(*里面的),可以使用下面的方式:
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来指定表名就可以解决这个问题了。
分享,成长,快乐
转载请注明blog地址:http://blog.csdn.net/fansy1990
java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index