2016-12-31 53 views
1

我有以下的Java代碼:棄用春AnnotationConfiguration

package modelo.util; 
 

 
import org.hibernate.cfg.AnnotationConfiguration; 
 
import org.hibernate.SessionFactory; 
 

 
public class HibernateUtil { 
 

 
    private static final SessionFactory sessionFactory; 
 
    
 
    static { 
 
     try { 
 
      sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 
 
     } catch (Throwable ex) { 
 
      // Log the exception. 
 
      System.err.println("Initial SessionFactory creation failed." + ex); 
 
      throw new ExceptionInInitializerError(ex); 
 
     } 
 
    } 
 
    
 
    public static SessionFactory getSessionFactory() { 
 
     return sessionFactory; 
 
    } 
 
}

請問以下行棄用上面的代碼:

import org.hibernate.cfg.AnnotationConfiguration; 
...= new AnnotationConfiguration().configure().buildSessionFactory(); 

我與hibernate 4.3.1Netbeans工作。
感謝

回答

2

的Javadoc說:

所有的功能已經轉移到Configuration

只是org.hibernate.cfg.Configuration取代org.hibernate.cfg.AnnotationConfiguration

Configuration.buildSessionFactory已被棄用,方法是採用ServiceRegistry。你必須配置一個ServiceRegistryBuilder

Configuration configuration = new Configuration(); 
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder(); 
builder.applySettings(configuration.getProperties()); 
ServiceRegistry serviceRegistry = builder.build(); 
sessionFactory = configuration.configure().buildSessionFactory(serviceRegistry); 
+0

我變了,現在只標註 –

+0

看的這張照片,對不起,我還沒有爲編輯我的最後評論 https://s24.postimg.org/wzuf43kxx/2222.png –

+0

>大家好, >林privilegies遵循本指南: > https://www.youtube.com/watch?v=Zb2cW25prCQ >我修改了你的建議,但是當我運行這個項目時出現這個: > https://s27.postimg.org/4vo8kthhf /2222.png –

相關問題