2015-12-15 58 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) –