Hibernate 4.3
Use hibernate4.3 time to get sessionfactory and not the same as before, online to see an article (StackOverflow inside), you can use the following way:
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 Sessions Getcurrentsession () {return factory.getcurrentsession (); public static void CloseSession {if (session!=null) {if
. IsOpen ()) {session.close (); }
}
}
}
Use the Standardserviceregister class to initialize our sessionfactory.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/
But when you use annotations, the following error occurs:
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.NativeMe Thodaccessorimpl.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.juni T.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)
The result has been a long time did not finish, but I used to 4.3 is OK, what's going on?
By contrast, I found that if you used a table (name= "T_user") in the model class, you would report such an error, but if I wrote @entity (name= "T_user"), that would be fine. This could be a bug in Hibernate4.3? This is not known, but if you use 4.3, then using entity directly to specify the table name will solve the problem.