'Why Hibernate cant create DB? SQLSyntaxErrorException and CommandAcceptanceException
I have three DBs, of which hibernate can only create 2/3. Can someone explain why hibernate can not create a third table "goods"
1: org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table goods (ID integer not null, brand varchar(255), desc varchar(255), model varchar(255), price float(53) not null, type varchar(255), primary key (ID)) engine=InnoDB" via JDBC Statement ---> at Models.Main.main(Main.java:18) <--- 2: Caused by: 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 'desc varchar(255), model varchar(255), price float(53) not null, type varchar(25' at line 1
<persistence
xmlns="https://jakarta.ee/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
version="3.0">
<persistence-unit name="OrderDB">
<properties>
<property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/orderdb"/>
<property name="jakarta.persistence.jdbc.user" value="root"/>
<property name="jakarta.persistence.jdbc.password" value="qwer1234"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
MAIN:
package Models;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import jakarta.persistence.Query;
import java.util.List;
import java.util.Scanner;
public class Main {
static EntityManagerFactory emf;
static EntityManager em;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
emf = Persistence.createEntityManagerFactory("OrderDB");
while (true) {
System.out.println("1: add user");
System.out.println("2: add goods");
System.out.println("3: create order");
System.out.println("4: view users");
System.out.println("5: view goods");
System.out.println("6: view orders");
System.out.print("-> ");
String s = sc.nextLine();
switch (s) {
case "1":
addUser(sc);
break;
case "2":
addGoods(sc);
break;
case "3":
createOrder(sc);
break;
case "4":
viewUsers();
break;
case "5":
viewGoods();
break;
case "6":
viewOrders();
break;
default:
return;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
em.close();
emf.close();
}
}
private static void addUser(Scanner sc) {
System.out.println("Enter information about user:");
System.out.print("Enter name: ");
String name = sc.nextLine();
System.out.print("Enter last name: ");
String lastName = sc.nextLine();
System.out.print("Enter age: ");
String strAge = sc.nextLine();
int age = Integer.parseInt(strAge);
System.out.print("Enter email: ");
String email = sc.nextLine();
System.out.print("Enter phone: ");
String phone = sc.nextLine();
try {
em = emf.createEntityManager();
em.getTransaction().begin();
User user = new User(name, lastName, age, email, phone);
em.persist(user);
em.getTransaction().commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
em.close();
}
}
private static void addGoods(Scanner sc) {
System.out.println("Enter information about product:");
System.out.print("Enter type: ");
String type = sc.nextLine();
System.out.print("Enter brand: ");
String brand = sc.nextLine();
System.out.print("Enter model: ");
String model = sc.nextLine();
System.out.print("Enter price: ");
double price = Double.parseDouble(sc.nextLine());
System.out.print("Enter description: ");
String desc = sc.nextLine();
try {
em = emf.createEntityManager();
em.getTransaction().begin();
Good good = new Good(type, brand, model, price, desc);
em.persist(good);
em.getTransaction().commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
em.close();
}
}
private static void createOrder(Scanner sc) {
System.out.println("Enter information about order:");
System.out.print("Enter goods id: ");
int goodID = Integer.parseInt(sc.nextLine());
System.out.print("Enter user id: ");
int userID = Integer.parseInt(sc.nextLine());
System.out.print("Enter order name: ");
String orderName = sc.nextLine();
try {
em = emf.createEntityManager();
em.getTransaction().begin();
Order order = new Order(goodID, userID, orderName);
em.persist(order);
em.getTransaction().commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
em.close();
}
}
private static void viewUsers() {
try {
em = emf.createEntityManager();
Query query = em.createQuery("SELECT u FROM User u", User.class);
List<User> list = (List<User>) query.getResultList();
for (User u : list)
System.out.println(u);
} catch (Exception e) {
e.printStackTrace();
} finally {
em.close();
}
}
private static void viewGoods() {
try {
em = emf.createEntityManager();
Query query = em.createQuery("SELECT g FROM Good g", Good.class);
List<Good> list = (List<Good>) query.getResultList();
for (Good g : list)
System.out.println(g);
} catch (Exception e) {
e.printStackTrace();
} finally {
em.close();
}
}
private static void viewOrders() {
try {
em = emf.createEntityManager();
Query query = em.createQuery("SELECT o FROM Order o", Order.class);
List<Order> list = (List<Order>) query.getResultList();
for (Order o : list)
System.out.println(o);
} catch (Exception e) {
e.printStackTrace();
} finally {
em.close();
}
}
}
enter code here
GOODS:
package Models;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "goods")
public class Good {
private int ID;
private String type;
private String brand;
private String model;
private double price;
private String desc;
public Good(String type, String brand, String model, double price, String desc) {
this.type = type;
this.brand = brand;
this.model = model;
this.price = price;
this.desc = desc;
}
public Good() {
}
@Id
@GeneratedValue
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "Good ID: " + ID + System.lineSeparator()
+ "Type: " + type + " " + brand + " " + model + System.lineSeparator()
+ "Price: " + price + System.lineSeparator()
+ "Description: " + desc;
}
}
Solution 1:[1]
I went through your code. The only fishy part is
private String desc;
"DESC" is reserved keyword in databases. If you look closely at the syntax(query) issued in your database, it will show you
CREATE TABLE goods (ID INTEGER NOT NULL,brand VARCHAR (255),DESC VARCHAR(255), model VARCHAR (255), price FLOAT (53) NOT NULL,TYPE VARCHAR(255), PRIMARY KEY (ID)) ENGINE = INNODB
You can see the "DESC" turns blue and shows that it is reserved keyword for "descending". Try changing it to something else like "descs" or you can define column and change it to something else like`
@Column(name="descs")
private String desc;
It will work.`
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Asgar |