title: Hibernate SQL Syntax error Generated query
body: <p>Update! : Removing this line:<code>configuration.addAnnotatedClass(Order.class);</code>from HibernateUtil.loadSessionFactory made hibernate able to execute without errors. But the order is still not saved to the database.</p>
  
  <p>When i save an object with <code>session.save()</code> it throws this error: 
  `</p>
  
  <blockquote>
    <p>java.sql.SQLSyntaxErrorException: 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 (orderNumber, totalPrice,
    orderId) values ('2019000009', 0.5, 1)' at line 1</p>
  </blockquote>
  
  <p>` It used to work fine, but after changing the code bit by bit trying to make hibernate work with relations of objects, I must have changed something that caused this.</p>
  
  <p>I don't write the query myself. It is generated by hibernate.
  Here's the object I want to save in the database:</p>
  
  <pre class="lang-java prettyprint-override"><code>
  @Entity
  @Table(name = "Order", schema = "products", catalog = "")
  public class Order {
  
      @OneToMany
      private List&lt;Product&gt; products = new ArrayList&lt;Product&gt;();
  
      @Id
      @Column( name = "orderId",  nullable = false)
      private int orderId;
  
      @Basic
      @Column(name = "orderNumber", nullable = false)
      private String orderNumber;
  
      @Basic
      @Column(name = "totalPrice", nullable = false)
      private double totalPrice;
  
      protected Order() {
      }
  
      public int getOrderId() {
          return this.orderId;
      }
  
      public void setOrderId(int orderId) {
          this.orderId = orderId;
      }
  
  
      public String getOrderNumber() {
          return this.orderNumber;
      }
  
      public double getTotalPrice() {
          return this.totalPrice;
      }
  
      public void setOrderNumber(String orderNumber) {
          this.orderNumber = orderNumber;
      }
  
      public void setTotalPrice(double totalPrice) {
          this.totalPrice = totalPrice;
      }
  
      @Override
      public boolean equals(Object o) {
          if (this == o) return true;
          if (o == null || getClass() != o.getClass()) return false;
          Order that = (Order) o;
          return orderId == that.orderId &amp;&amp;
                  Objects.equals(orderNumber, that.orderNumber) &amp;&amp;
                  Objects.equals(totalPrice, that.totalPrice);
      }
  
      @Override
      public int hashCode() {
          return Objects.hash(orderId, totalPrice, orderNumber);
      }
  
  
      public List&lt;Product&gt; getProducts() {
          return products;
      }
  
      public void setProducts(List&lt;Product&gt; products) {
          this.products = products;
      }
  }
  </code></pre>
  
  <p>Hibernate.cfg.xml:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;property name="connection.url"&gt;jdbc:mysql://localhost:3306/products?serverTimezone=UTC&lt;/property&gt;
          &lt;property name="connection.driver_class"&gt;com.mysql.cj.jdbc.Driver&lt;/property&gt;
          &lt;property name="connection.username"&gt;root&lt;/property&gt;
          &lt;property name="connection.password"&gt;****&lt;/property&gt;
          &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
          &lt;property name="hibernate.connection.pool_size"&gt;1&lt;/property&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
  
  
          &lt;mapping class="nu.educom.calculateChange.Product"/&gt;
          &lt;mapping class="nu.educom.calculateChange.Order"/&gt;
          &lt;mapping resource="Order.hbm.xml"/&gt;
  
  
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>HibernateUtil:</p>
  
  <pre class="lang-java prettyprint-override"><code>
  public class HibernateUtil {
  
      private static SessionFactory sessionFactory = loadSessionFactory();
  
      static {
          try{
              loadSessionFactory();
          }catch(Exception e){
              System.err.println("Exception while initializing hibernate util.. ");
              e.printStackTrace();
          }
      }
  
      public static SessionFactory loadSessionFactory(){
  
          Configuration configuration = new Configuration();
          configuration.configure("/hibernate.cfg.xml");
          configuration.addAnnotatedClass(Order.class);
          configuration.addAnnotatedClass(Product.class);
          ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                  configuration.getProperties()).build();
          return configuration.buildSessionFactory(serviceRegistry);
      }
  
      public static Session getSession() throws HibernateException {
          Session retSession=null;
          try {
              retSession = sessionFactory.openSession();
          }catch(Throwable t){
              System.err.println("Exception while getting session.. ");
              t.printStackTrace();
          }
          if(retSession == null) {
              System.err.println("session is discovered null");
          }
  
          return retSession;
      }
  
      public static long insertOrder(Object object) {
          loadSessionFactory();
          Session session = getSession();
          Integer lastInsertId = 1;
          Transaction tx = null;
          try {
              tx = session.beginTransaction();
              lastInsertId = (Integer) session.save(object);
              tx.commit();
          } catch (HibernateException ex) {
              if (tx != null) {
                  tx.rollback();
              }
          } finally {
              session.close();
          }
  
          return lastInsertId;
      }
  }
  </code></pre>
  
  <p>Order.hbm.xml:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD//EN"
          "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;
  
  &lt;hibernate-mapping&gt;
      &lt;class name = "nu.educom.calculateChange.Order" table = "order"&gt;
          &lt;id name = "orderId" type = "int" column="orderId"&gt;
              &lt;generator class="native"/&gt;
          &lt;/id&gt;
          &lt;property name="orderNumber" column="orderNumber" type="string"/&gt;
          &lt;property name="totalPrice" column="totalPrice" type="double"/&gt;
  
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Lastly, the call to save the order:</p>
  
  <pre class="lang-java prettyprint-override"><code>public void processOrder() {
          Order order = new Order();
          order.setTotalPrice(this.getTotal());
          order.setOrderNumber("2019000009");
          order.setOrderId(1);
  
  
          for (Product product: this.productsAddedToRegister) {
              order.getProducts().add(product);
          }
          HibernateUtil.loadSessionFactory();
          long orderId = HibernateUtil.insertOrder(order);
  
  
      }
  </code></pre>