Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java hibernate多个持久对象?_Java_Mysql_Hibernate - Fatal编程技术网

java hibernate多个持久对象?

java hibernate多个持久对象?,java,mysql,hibernate,Java,Mysql,Hibernate,我在通过hibernate查询表时遇到问题 我有一个客户POJO映射到hibernate 我的文件: Customers.java Customers.hbm.xml hibernate.cfg.xml 我可以很好的查询这个对象,列表,添加,删除等等 现在我添加了另一个POJO类Order,我还在数据库中为它创建了一个表,如Customer POJO。我做了一份精确的副本,并改为“订单”,其中“客户”是 现在,来自我的主要客户。我运行以下命令: public void listCustomer

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

我有一个客户POJO映射到hibernate

我的文件:

Customers.java
Customers.hbm.xml
hibernate.cfg.xml
我可以很好的查询这个对象,列表,添加,删除等等

现在我添加了另一个POJO类Order,我还在数据库中为它创建了一个表,如Customer POJO。我做了一份精确的副本,并改为“订单”,其中“客户”是

现在,来自我的主要客户。我运行以下命令:

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(); 
          }
       }
一切正常,我从数据库中获取客户表中的所有条目

现在,当我试图查询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(); 
          }
       }
它不起作用。。无法查询订单表。若有人能指出错误的正确方向,我将不胜感激

谢谢

错误:

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)

单词顺序是ORDER BY中使用的保留关键字。我不知道有什么方法可以在HQL中正确地避开它。重命名将是更安全的选择。

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

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

团体

DP-不确定,但出现在此处:

另外,我在org.hibernate.dialent.mysqlinnodbdialent之前使用了org.hibernate.dialent.mysqlinnodbdialent,而不是org.hibernate.dialent.mysql5innodbdialent作为我的hibernate.dialen属性

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

这就是诀窍

啊,可能是这样的,我很确定是这样的。例外情况是这样的:只需按照我给出的[order]而不是order编写实体名称。因此,sql不将其视为关键字,您将看到order table已成功创建Hanks,请您编辑问题并添加信息。