简体   繁体   English

为什么 Hibernate 无法创建数据库? SQLSyntaxErrorException 和 CommandAcceptanceException

[英]Why Hibernate cant create DB? SQLSyntaxErrorException and CommandAcceptanceException

I have three DBs, of which hibernate can only create 2/3.我有3个DB,其中hibernate只能创建2/3。 Can someone explain why hibernate can not create a third table "goods"有人可以解释为什么 hibernate 不能创建第三个表“商品”

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; 1: org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing table goods (ID integer not null, brand varchar(255), desc varchar(255), model varchar(255), price float(53)不是 null,类型 varchar(255),主键 (ID)) engine=InnoDB" 通过 JDBC 语句 ---> 在 Models.Main.main(Main.java:18) <--- 2: Caused by: java.88368828.236788.236788 SQLSyntaxErrorException:您的 SQL 语法有错误; 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查看与您的 MySQL 服务器版本对应的手册,了解在 '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;
    }
}

I went through your code.我检查了你的代码。 The only fishy part is唯一可疑的部分是

private String desc; 

"DESC" is reserved keyword in databases. “DESC”是数据库中的保留关键字。 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".你可以看到“DESC”变成蓝色,表明它是“降序”的保留关键字。 Try changing it to something else like "descs" or you can define column and change it to something else like`尝试将其更改为“descs”之类的其他内容,或者您可以定义列并将其更改为其他内容,例如`

@Column(name="descs")
private String desc;

It will work.`它会起作用的。`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring execption:org.hibernate.tool.schema.spi.CommandAcceptanceException: 执行 DDL 时出错“create table post - Spring execption:org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table post org.hibernate.tool.schema.spi.CommandAcceptanceException:执行 DDL“CREATE FUNCTION”时出错 - org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL “CREATE FUNCTION” org.hibernate.tool.schema.spi.CommandAcceptanceException:通过 JDBC 语句执行 DDL“创建表用户(///)”时出错 - org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table user (///)" via JDBC Statement org.hibernate.tool.schema.spi.CommandAcceptanceException:执行 DDL 时出错“创建表客户 - org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table customer Hibernate 无法创建表:org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table PSQLException - Hibernate can't create a table: org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table PSQLException Hibernate Where子句中的SQLSyntaxErrorException - SQLSyntaxErrorException in Hibernate Where clause Hibernate MySQL Cant创建表 - Hibernate MySQL Cant create table 如何在Hibernate中创建数据库 - How to create DB in Hibernate 休眠:记录到数据库后无法获取数据 - Hibernate: cant get data after record to DB Spring 引导 + JPA + Hibernate CommandAcceptanceException:执行 DDL 时出错 - Spring Boot + JPA + Hibernate CommandAcceptanceException: Error executing DDL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM