java hibernate多个持久对象?

时间:2013-12-10 17:53:41

标签: java mysql hibernate

我在通过hibernate查询表时遇到问题。

我有一个客户POJO映射到hibernate。

我的档案:

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

我可以很好地查询这个对象,列表,添加,删除等等。

现在我添加了另一个POJO类Order,我还在数据库中创建了一个表,如Customer 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(); 
          }
       }

并且一切正常,我从数据库中获取Customer表中的所有条目。

现在,当我尝试查询Order表时,通过制作第二个函数(用于测试)

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(); 
          }
       }

它不起作用..无法查询Order表。如果有人可以指出错误的正确方向,我将不胜感激。

感谢

错误:

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)

4 个答案:

答案 0 :(得分:0)

ORDER 这个词是ORDER BY中使用的保留关键字。我不知道有任何办法在HQL中正确地逃避它。重命名将是更安全的选择。

答案 1 :(得分:0)

您需要将表名从“ORDER”更改为其他非保留关键字的名称。

答案 2 :(得分:0)

正如其他人所说,ORDER是一个保留关键字,在我的情况下,我使用了几个保留字:

<强>组

DP - 不确定但显示在此处:List of SQL reserved words

我还使用org.hibernate.dialect.MySQLInnoDBDialect(在org.hibernate.dialect.MySQL之前)而不是org.hibernate.dialect.MySQL5InnoDBDialect作为我的hibernate.dialect属性。

答案 3 :(得分:0)

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

这就是诀窍。