2012-03-06 124 views
0
 try 
     { 
      System.out.println("openTxCoreSession() start..."); 
      TxCoreSessionFactory sessionFactory = 
       TxCoreSessionFactory.getInstance("txcore.cfg.xml"); 
      System.out.println("Session factory created...."); 

      Session session = sessionFactory.openSession(); 
      System.out.println("session created"); 
      return session; 
     } 

     catch (Exception e) 
     { 
      System.out.println(e.getMessage()); 
      e.printStackTrace(); 
      return null; 
     } 
+1

你是否得到某種異常?如果是這樣,那是什麼? – 2012-03-06 05:18:28

+0

TxCoreSessionFactory sessionFactory = TxCoreSessionFactory.getInstance(「txcore.cfg.xml」); 上面的行後面不打印「會話工廠創建」,並且不會拋出任何異常。 – 2012-03-06 05:29:19

+0

你可以發佈你的「txcore.cfg.xml」文件嗎? – Rocky 2012-03-06 05:30:21

回答

1

休眠不容易開始,需要一點時間/精力。

對於感到困惑的人來說,MyEclipse或JBoss Hibernate Tools並沒有什麼不同。

在服務器平臺上使用Hibernate的主要原因是擺脫複雜的JDBC地獄洞。你認爲你需要一個對象關係映射解決方案的唯一原因是爲了獲得代碼中的一些整潔以及內置於設計中的舊可重用性。

此外,下面的作品適合我。

  if (sessionFactory == null) { 
       try { 
        String jdbcProperty = "jdbc:mysql://"+Globals.DBSERVER+"/MyDB" ; 
        Configuration configuration = new Configuration().configure() ;     
        sessionFactory = configuration.buildSessionFactory(new ServiceRegistryBuilder() 
           .buildServiceRegistry()); 

       } catch (Exception e) { 
        log.fatal("Unable to create SessionFactory for Hibernate"); 
        log.fatal(e.getMessage()); 
        log.fatal(e); 
        e.printStackTrace(); 
       } 
      } 

我的eclipse項目的src文件夾中的Hibernate.properties。

hibernate.connection.driver_class=com.mysql.jdbc.Driver 
hibernate.connection.url=jdbc:mysql://localhost/MyDB 
hibernate.connection.username=MYROOT 
hibernate.connection.password=myPASSWORD 
hibernate.connection.pool_size=2 
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect 

此外,還要確保你的XML配置文件(txcore.cfg.xml)是在應用程序的類路徑。

+0

是的,但是如果它沒有找到txcore.cfg.xml,這應該會拋出一個錯誤。沒有? – 2012-03-06 06:40:27

+0

是的,你需要確保它在你的eclipse項目的src中。 – Siddharth 2012-03-06 07:07:02

+0

我已經把配置文件放在src中,問題仍然是一樣的.... – 2012-03-06 09:43:22