当前位置:天才代写 > tutorial > JAVA 教程 > 利用hibernate4.3的时候获取SessionFactory和之前纷歧样

利用hibernate4.3的时候获取SessionFactory和之前纷歧样

2017-11-02 08:00 星期四 所属: JAVA 教程 浏览:50

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来指定表名就可以办理这个问题了。

 

    关键字:


天才代写-代写联系方式