2013-12-10 138 views
0

我有問題通過休眠查詢表。java hibernate多持久化對象?

我有一個客戶POJO映射到休眠。

我的文件:

Customers.java 
Customers.hbm.xml 
hibernate.cfg.xml 

我可以查詢這個對象就好了,名單,添加,刪除等

現在我又增加了POJO類,秩序,這也是我創建了一個表在像客戶POJO這樣的數據庫中。 (我做了一個確切的副本,並更改​​爲「訂單」,其中「客戶」是)。

現在,從我的主類ManageCustomer中。我運行這個命令:

public void listCustomer(){ 
      Session session = sessionFactory.openSession(); 
      Transaction tx = null; 
      try{ 
      tx = session.beginTransaction(); 
      List customers = session.createQuery("FROM Customer").list(); 
      for (Iterator iterator = 
           customers.iterator(); iterator.hasNext();){ 
       Customer customer = (Customer) iterator.next(); 

       String tmpMessage = customer.getFirstName().toString(); 
       System.out.println("First Name: "+customer.getFirstName()+", Credit: "+customer.getCreditBalance()+"\r\n"); 

      } 
      tx.commit(); 
      }catch (HibernateException e) { 
      if (tx!=null) tx.rollback(); 
      e.printStackTrace(); 
      }finally { 
      session.close(); 
      } 
     } 

和一切工作正常,我從數據庫中獲取客戶表中的所有條目。

現在,當我試圖查詢訂單表,通過使第二功能(測試)

public void listCustomer2(){ 
      Session session = sessionFactory.openSession(); 
      Transaction tx = null; 
      try{ 
      tx = session.beginTransaction(); 
      List orders = session.createQuery("FROM Order").list(); 
      for (Iterator iterator = 
           orders.iterator(); iterator.hasNext();){ 
       Order order = (Order) iterator.next(); 

       String tmpMessage = order.getFirstName().toString(); 
       System.out.println("First Name: "+order.getFirstName()+", Credit: "+order.getCreditBalance()+"\r\n"); 

      } 
      tx.commit(); 
      }catch (HibernateException e) { 
      if (tx!=null) tx.rollback(); 
      e.printStackTrace(); 
      }finally { 
      session.close(); 
      } 
     } 

它不工作..不能查詢順序表。如果有人能夠指出錯誤的正確方向,我們將不勝感激。

感謝

錯誤:

ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER order0_' at line 1 
org.hibernate.exception.SQLGrammarException: could not extract ResultSet 
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82) 
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) 
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) 
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110) 
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:61) 
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2040) 
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1837) 
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1816) 
    at org.hibernate.loader.Loader.doQuery(Loader.java:900) 
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342) 
    at org.hibernate.loader.Loader.doList(Loader.java:2526) 
    at org.hibernate.loader.Loader.doList(Loader.java:2512) 
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342) 
    at org.hibernate.loader.Loader.list(Loader.java:2337) 
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495) 
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:356) 
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195) 
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1269) 
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101) 
    at com.hexs.net.ManageCustomer.listEmployees2(ManageCustomer.java:726) 
    at com.hexs.net.ManageCustomer.handlePlayerListRequest(ManageCustomer.java:498) 
    at com.hexs.net.ManageCustomer$ChatIncomingReader.run(ManageCustomer.java:552) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER order0_' at line 1 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
    at com.mysql.jdbc.Util.getInstance(Util.java:386) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4187) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815) 
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155) 
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322) 
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:56) 

回答

0

ORDER是在ORDER BY使用一個保留關鍵字。我不知道有什麼方法可以在HQL中正確地轉義它。重命名將是更安全的選擇。

+0

啊,可以做到這一點 – user2556304

+0

我很確定它是。例外情況如此:) – Bart

0

您需要將表名從'ORDER'更改爲其他不是保留關鍵字的其他名稱。

0

正如其他人所說,順序是保留關鍵字,在我的情況,我用幾個保留字:

DP集團 - 不知道,但這裏出現:List of SQL reserved words

另外我使用的是org.hibernate.dialect.MySQLInnoDBDialect(在org.hibernate.dialect.MySQL之前)而不是org.hibernate.dialect.MySQL5InnoDBDialect作爲我的hibernate.dialect屬性。

0
@Entity 
@Table(name = "[order]") 
public class Order implements Serializable { 
------------ 
} 

那就是訣竅。

+0

你能解釋一下這是如何解決這個問題的? – rjdkolb

+0

只需寫出實體名稱,就像我給出的「[order]」而不是「order」。所以,SQL不把它當作關鍵字,你會看到訂單表已成功創建 –

+0

謝謝,你可以請編輯的問題,並添加信息。 – rjdkolb