2015-12-15 121 views
2

林在該行獲得一個空值sessionFactory變量:休眠5:SessionFactory的是空

sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); 

這是全班同學:

import javax.imageio.spi.ServiceRegistry; 
import javax.transaction.Transaction; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.boot.MetadataSources; 
import org.hibernate.boot.registry.StandardServiceRegistry; 
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 
import org.hibernate.cfg.Configuration; 

import ch.makery.model.Employee; 

public class HelloWorld { 
    protected void setUp() throws Exception { 
    } 

    public static void main(String[] args) { 

     SessionFactory sessionFactory = null; 

     // A SessionFactory is set up once for an application! 
     final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() 
       .configure() // configures settings from hibernate.cfg.xml 
       .build(); 
     try { 
      sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); 
     } 
     catch (Exception e) { 
      // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory 
      // so destroy it manually. 
      StandardServiceRegistryBuilder.destroy(registry); 
     } 

     Session session = sessionFactory.openSession(); 
     //employee = new Employee(); 

     session.beginTransaction(); 
     session.save(new Employee()); 
     session.getTransaction().commit(); 
     session.close(); 
    } 
} 

這是我的Hibernate相關的文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="hibernate.connection.password">manolete</property> 
     <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/employee</property> 
     <property name="hibernate.connection.username">root</property> 
     <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> 
     <property name="hbm2ddl.auto">create</property> 
     <mapping resource="src/ch/makery/model/Employee.hbm.xml" /> 
    </session-factory> 
</hibernate-configuration> 
<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<!-- Generated 14-dic-2015 21:00:04 by Hibernate Tools 3.4.0.CR1 --> 
<hibernate-mapping> 
    <class name="ch.makery.model.Employee" table="EMPLOYEE"> 
     <id name="id" type="int"> 
      <column name="ID" /> 
      <generator class="assigned" /> 
     </id> 
     <property name="firstName" type="java.lang.String"> 
      <column name="FIRSTNAME" /> 
     </property> 
     <property name="lastName" type="java.lang.String"> 
      <column name="LASTNAME" /> 
     </property> 
    </class> 
</hibernate-mapping> 
package ch.makery.model; 

public class Employee { 
    private int id; 
    private String firstName,lastName; 

    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 
    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 
    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

} 

我不使用Spring,因爲我只是創建一個桌面應用程序。

回答

0

該方法buildSessionFactory是版本中的deprecated,它被替換爲新的API。如果你正在使用Hibernate的4.3.0及以上嘗試寫這樣的結構:

Configuration configuration = new Configuration().configure(); 
configuration.configure("your_path_hibernate_.cfg.xml"); 
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); 
sessionFactory = configuration.buildSessionFactory(ssrb.build()); 
+0

但快速啓動寫着'buildSessionFactory()'它不會被棄用:http://docs.jboss.org/hibernate/orm/5.0/quickstart/html/ – ziiweb

+0

@ziiweb看看這個[鏈接](https://github.com/hibernate/hibernate-orm/blob/master/hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java) – Abdelhak

1

當我更新我的Hibernate代碼從v4.1.8到v5.0.6我也有一個問題,我SessionFactory實例是空值。通過打印堆棧跟蹤信息,我發現我需要在我的構建路徑中包含可選的c3p0罐。

我的第一個建議將直接從您的catch塊打印堆棧跟蹤。追蹤將爲您解決問題提供一個良好的起點。所以,你的catch塊看起來像:

catch (Exception e) { 
     // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory 
     // so destroy it manually. 
     StandardServiceRegistryBuilder.destroy(registry); 
     e.printStackTrace(); 
    } 

然而,我注意到,你沒有在配置文件中定義的hibernate.dialect屬性。雖然此屬性不是強制性的,但Hibernate User Guide指出,可能存在「某些原因」,hibernate無法確定您正在使用哪種方言。我的第二個建議將直接在配置文件中使用hibernate.dialect設置來定義數據庫方言。

因爲你原來的配置文件後表明,你正在使用的MySQL數據庫方言,嘗試添加在此方言屬性節點會話工廠節點:

<property name="dialect">org.hibernate.dialect.MySQLDialect</property> 

如果你正在使用MySQL 5。 X然後用

<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 

確保可選c3p0罐子在您的構建路徑。

祝你好運!

0
private static SessionFactory sessionFactory = createSessionFactory(); 

private static SessionFactory createSessionFactory() { 
    if (sessionFactory == null) { 
     StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()     .configure("hibernate.cfg.xml").build(); 
     Metadata metaData = new MetadataSources(standardRegistry).getMetadataBuilder().build(); 
     sessionFactory = metaData.getSessionFactoryBuilder().build(); 
    } 
    return sessionFactory; 
} 

public static SessionFactory getSessionFactory() { 
    return sessionFactory; 
} 

public static void shutdown() { 
    sessionFactory.getCurrentSession().close(); 
} 
+0

歡迎來到Stack Overflow!雖然這段代碼可能會解決問題,但它並不能解釋爲什麼或如何回答問題。請[請爲您的代碼添加解釋](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers),因爲這確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 **舉報人/評論者:** [僅限代碼解答,例如downvote,請勿刪除!](// meta.stackoverflow.com/a/260413/2747593) –