2011-05-22 241 views
0

我已經建立了Hibernate插件到NetBeans和無法連接到MySQL DBS - 代碼:休眠連接問題

package client; 

import org.hibernate.*; 
import org.hibernate.cfg.*; 
import java.util.*; 

public class HiberTest { 

    private static SessionFactory sessionFactory; 

    private int id; 

    public int getId() { 
    return this.id; 
    } 

    public void setId(int id) { 
    this.id = id; 
    } 

    protected static void setUp() throws Exception { 
     // A SessionFactory is set up once for an application 
     sessionFactory = new Configuration() 
       .configure() // configures settings from hibernate.cfg.xml 
       .buildSessionFactory(); 
    } 

    public static void main(String[] args) throws Exception { 
    setUp(); 
    Session session = HibernateUtils.getSessionFactory().openSession(); 
    session.beginTransaction(); 
    Query q = session.createQuery("CREATE TABLE test(myInt int not null)"); 
     List resultList = q.list(); 
     System.out.println(resultList); 
     session.getTransaction().commit(); 
     session.close(); 
    } 

} 

HibernateUtils.class

package client; 

import org.hibernate.cfg.*; 
import org.hibernate.SessionFactory; 

public class HibernateUtils { 

    private static final SessionFactory sessionFactory = buildSessionFactory(); 
    private static SessionFactory buildSessionFactory() { 
     try { 
      // Create the SessionFactory from hibernate.cfg.xml 
      return new Configuration().configure().buildSessionFactory(); 
     } catch (Throwable ex) { 
      // Make sure you log the exception, as it might be swallowed 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 
    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 

} 

的錯誤:

May 22, 2011 12:40:33 PM org.hibernate.hql.ast.ErrorCounter reportError 
SEVERE: line 1:1: unexpected token: CREATE 
Exception in thread "main" java.lang.IllegalArgumentException: node to traverse cannot be null! 
     at org.hibernate.hql.ast.util.NodeTraverser.traverseDepthFirst(NodeTraverser.java:31) 
     at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:254) 
     at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157) 
     at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111) 
     at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77) 
     at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56) 
     at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72) 
     at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133) 
     at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112) 
     at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623) 
     at client.HiberTest.main(HiberTest.java:36) 
Java Result: 1 

請幫我解決這個問題,這是我處理hibernate的一個難點。 所有CFG更改後我desided張貼cfg.xml中和的hbm.xml可能有人會在這裏建議的東西,他們是: cfg.xml中

<?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.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.url">jdbc:mysql://myhost:3306/wwwgeeksearthcom_geeksearth_test</property> 
    <property name="hibernate.connection.username">user_name</property> 
    <property name="hibernate.connection.password">**********</property> 
    <property name="hibernate.show_sql">true</property> 
    <!--<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTransactionFactory</property>--> 
    <mapping resource="hibernate.hbm.xml"/> 
    </session-factory> 
</hibernate-configuration> 

和的hbm.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
<class name="HiberTest" table="guests"> 
    <id name="id" column="g_id"> 
     <generator class="native"/> 
    </id> 
</class> 
</hibernate-mapping> 

回答

1

日誌已經說過,映射文件hibernate.hbm.xml<class>的格式不正確,因爲有下列錯誤:

的ELE內容(meta *,subselect?,cache?,synchronize *,comment?,tuplizer *,(id | composite-id),discriminator?,natural-id?,(())是( 不完整的,它必須匹配 版本|時間戳),(屬性|多對一|一對一|組件|動態組件|屬性|任意|地圖|集|列表|袋| idbag |數組|原始數組)| ,( (join,subclass *)| joined-subclass * | union-subclass *),loader?,sql-insert?,sql-update?,sql-delete?,filter *,resultset *,(query | sql-query) *)」

此錯誤消息表明您違反了<class>的一些要求,例如:

  • 它只能包含括號內的子元素
  • *號表示子元素可以出現零次或多次。
  • The?符號表示子元素可以出現零次或一次。
  • 沒有*和?必須包括該子元素並且僅包括一次(例如,id)

可以參考this來知道如何讀取DTD元素語法。

+0

呀,謝謝 - 我會檢查我的映射hbm.xml文件中。 – 2011-05-22 07:02:16

+0

現在我確定U只會瀏覽上面添加的XML文件,並告訴我這是什麼問題。我會很感激這一點。我試圖解決這個問題,但...錯誤仍然在這裏 – 2011-05-22 07:23:21

+0

你的xml應該被驗證。我認爲這是因爲其他錯誤。這次的錯誤信息是什麼? – 2011-05-22 07:31:41

0

,你應該爲這個菜單與NetBeans速效只需右鍵點擊XML的.xml文件check xmlvalidate xml使用和檢查