3

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();
    }
}

这是我的休眠相关文件:

<?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,因为我只是创建了一个桌面应用程序。

4

3 回答 3

1

当我将我的 Hibernate 代码从 v4.1.8 更新到 v5.0.6 时,我也遇到了我的SessionFactory实例为空的问题。通过打印堆栈跟踪,我发现我需要c3p0在构建路径中包含可选的 jar。

我的第一个建议是直接从你的 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 用户指南指出,可能有“某种原因”hibernate 无法确定您正在使用哪种方言。我的第二个建议是使用设置直接在配置文件中定义数据库方言hibernate.dialect

由于您的原始配置文件帖子表明您正在使用 MySQL 数据库方言,请尝试将此方言属性节点添加到会话工厂节点:

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

如果您使用的是 MySQL 5.x,请使用

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

确保可选的c3p0jars 在你的构建路径中。

祝你好运!

于 2015-12-31T19:48:59.460 回答
1
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();
}
于 2016-10-22T17:30:22.100 回答
1

该方法来自发行版,并buildSessionFactory已替换为新的 API。如果您使用的是 hibernate 4.3.0 及更高版本,请尝试编写如下配置:deprecatedhibernate 4

Configuration configuration = new Configuration().configure();
configuration.configure("your_path_hibernate_.cfg.xml");
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
 sessionFactory = configuration.buildSessionFactory(ssrb.build());
于 2015-12-15T12:59:55.567 回答