小编典典

错误:您在Hibernate中的SQL语法中有错误

hibernate

我从数据库中删除记录时遇到问题。几个小时以来,我一直在努力弄清自己在做错什么。我在这里搜索了一些主题,但没有一个回答我在做什么错。我在数据库中制作了这3个表:

CREATE TABLE IF NOT EXISTS `dat1`.`M_CUSTOMER_LIST` (
  `CUSTOMER_LIST_ID` int(4) NOT NULL primary KEY AUTO_INCREMENT,
  `C_NAME` varchar(45) NOT NULL,
  `C_LAST_NAME` varchar(45) NOT NULL,
  `C_P_CODE` varchar(12) NOT NULL,
  `C_MAIL` varchar(45) NOT NULL,
  `C_PHONE_NR` varchar(12) NOT NULL,
  `C_USER_TYPE` varchar(45) NOT NULL,
  `C_PASSWORD` varchar(45) NOT NULL
);

CREATE TABLE IF NOT EXISTS `dat1`.`M_SERVICES` (
 `M_SERVICE_ID` int(4) NOT NULL primary KEY AUTO_INCREMENT,
 `SERVICE_NAME` varchar(45) NOT NULL,
 `SERVICE_PRICE` INT(4) NOT NULL,
 `SERVICE_DESCRIPTION` int(60) NOT NULL
);

CREATE TABLE IF NOT EXISTS `dat1`.`ORDER` (
  `ORDER_ID` int(4) NOT NULL primary KEY AUTO_INCREMENT,
  `ORDER_DATE` varchar(45) NOT NULL,
  `M_CUST` int(4) NOT NULL,
  `M_SERV` int(4) NOT NULL,
  foreign key (`M_SERV`) references `M_SERVICES`(`M_SERVICE_ID`),
  foreign key (`M_CUST`) references `M_CUSTOMER_LIST`(`CUSTOMER_LIST_ID`)
);

这是我的hibernate映射文件:

 <?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="dat3.MCustomerList" table="m_customer_list">
     <id column="CUSTOMER_LIST_ID" name="customerList_id" type="int">
       <generator class="native"/>
     </id>
    <property column="C_NAME" name="c_name" type="string"/>
    <property column="C_L_NAME" name="c_l_name" type="string"/>
    <property column="C_P_CODE" name="c_p_code" type="string"/>
    <property column="C_EMAIL" name="c_email" type="string"/>
    <property column="C_PHONE_NR" name="c_phone_nr" type="string"/>
    <property column="C_USER_TYPE" name="c_user_type" type="string"/>
    <property column="C_PASSWORD" name="c_password" type="string"/>
  </class>
  <class name="dat3.M_Services" table="m_services">
    <id column="M_SERVICE_ID" name="m_service_id" type="int">
  <generator class="native"/>
    </id>
    <property column="SERVICE_NAME" name="service_name" type="string"/>
    <property column="SERVICE_PRICE" name="service_price" type="int"/>
    <property column="SERVICE_DESCRIPTION" name="service_description" type="string"/>
  </class>
  <class name="dat3.Order" table="order">
    <id column="ORDER_ID" name="order_id" type="int">
  <generator class="native"/>
    </id>
    <property column="ORDER_DATE" name="order_date" type="date"/>
    <many-to-one class="dat3.MCustomerList" column="M_CUST" name="customerList" not-null="true"/>
    <many-to-one class="dat3.M_Services" column="M_SERV" name="m_service" not-null="true"/>
   </class>
</hibernate-mapping>

因此,当我尝试从jTable中删除一条记录时,它给了我一个错误。这是我用来从jTable和数据库中删除数据的代码:

    if ((this.jTable1.getSelectedRow() != -1) && (this.jTable1.getValueAt(this.jTable1.getSelectedRow(), 0) != ""))
    {
        ServiceCheck serviceCheck = new ServiceCheck();
        serviceCheck.deleteOrder(Integer.valueOf(this.jTable1.getModel().getValueAt(this.jTable1.getSelectedRow(), 0).toString()).intValue());
    }
    else
    {
      JOptionPane.showMessageDialog(null, "Error");
    }

ServiceCheck.java

public void deleteOrder(int id){
  Session session = factory.openSession();
  Transaction tx = null;
  try{
     tx = session.beginTransaction();
     Order order = (Order)session.get(Order.class, Integer.valueOf(id)); 
     session.delete(order); 
     JOptionPane.showMessageDialog(null, "You have successfully deleted your order");
     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_ where order0_.ORDER_ID=2' at line 1

任何帮助将不胜感激!


阅读 248

收藏
2020-06-20

共1个答案

小编典典

ORDER保留的SQL关键字。用其他名称创建ORDER表

2020-06-20