3

sessionFactoryこの行で variableの null 値を取得しています:

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 は使用していません。

4

3 に答える 3

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はリリースdeprecatedhibernate 4ものであり、新しい 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());
于 2015-12-15T12:59:55.567 に答える
1

Hibernate コードを v4.1.8 から v5.0.6 に更新したとき、SessionFactoryインスタンスが null になるという問題がありました。スタック トレースを出力すること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 が判断できない可能性があると記載されています。私の2 番目の提案は、データベースの方言を設定ファイルで直接定義することhibernate.dialectです。

元の構成ファイルの投稿は、MySQL データベースの方言を使用していることを示唆しているため、この方言プロパティ ノードを session-factory ノードに追加してみてください。

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

MySQL 5.x を使用している場合は、

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

オプションのc3p0jar がビルド パスにあることを確認してください。

頑張ってください!

于 2015-12-31T19:48:59.460 に答える