2012-01-12 6 views
17

私はhibernate.cfg.xmlファイルを持ちたくありません。むしろ、以下のように私のコードを通してすべての設定をしたいと思っています。設定のためにhibernate.cfg.xmlファイルを持っていることは必須ですか

private static final SessionFactory factory; 
private static final Properties properties; 

static 
{ 
    properties = new Properties(); 
    properties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); 
    properties.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/books"); 
    properties.setProperty("hibernate.connection.username", "jhtp7"); 
    properties.setProperty("hibernate.connection.password", "password"); 
    properties.setProperty("hibernate.show_sql", "com.mysql.jdbc.Driver"); 
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); 

    factory = new Configuration().setProperties(properties).configure(). 
          buildSessionFactory(); 
} 

私は上記のアプローチを試みた。しかし、私は、ハイバネートが "./hibernate.cfg.xml file missing"という例外をスローする問題に直面しています。

ファイルhibernate.cfg.xmlを保管することは本当に必須ですか?事前に

おかげで

答えて

20

私はそれがConfigurationオブジェクトのあなたのconfigure()コールのためだと思います。それを削除しようとすると、休止状態は存在しないファイルを検索しません。基本的には、propertiesオブジェクトを介して必要なすべてのプロパティを設定しているので、Hibernateにhibernate.cfg.xmlファイルを探すように実際に要求する必要はありません。これは、configure()メソッドとまったく同じです。

10

いいえ、私は設定XMLが必須であるとは思いません。あなたの問題を解決するには、org.hibernate.cfg.Configurationクラスを使う必要があると思います。このリンクを見てください:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html

は基本的に、あなたが)あなたのSessionFactory、あなただけ言う、cfg.buildSessionFactoryを(作成するために、次に

Configuration cfg = new Configuration() 
.setProperty("hibernate.dialect", "com.mysql.jdbc.Driver") 
.setProperty("hibernate.connection.datasource", "jdbc:mysql://localhost:3306/books") 
.setProperty("hibernate.order_updates", "true"); 

のようなものを必要とします。

6

いいえ、hibernate.cfg.xmlを使用することは必須ではありません。 .configure()を使用しないでください。 .configure()を使用する場合、Hibernateはhibernate.cfg.xmlを探します。

public class HibernateUtil { 
    private static SessionFactory sessionFactory ; 
    static { 
     Configuration configuration = new Configuration(); 

     configuration.addAnnotatedClass (org.gradle.Person.class); 
     configuration.setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver"); 
     configuration.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate");         
     configuration.setProperty("hibernate.connection.username", "root");  
     configuration.setProperty("hibernate.connection.password", "root"); 
     configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); 
     configuration.setProperty("hibernate.hbm2ddl.auto", "update"); 
     configuration.setProperty("hibernate.show_sql", "true"); 
     configuration.setProperty(" hibernate.connection.pool_size", "10"); 

     StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); 
     sessionFactory = configuration.buildSessionFactory(builder.build()); 
    } 
    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 
} 
+0

コンソールでsqlクエリをログに記録する場合は、次のようにします。configuration.setProperty( "hibernate.show_sql"、 "true"); – Deepak

+0

次にマッピングを指定する方法は? – digz6666

+2

このようにして:configuration.addAnnotatedClass(org.gradle.Person.class); – Deepak

関連する問題