我有三个DBs,其中hibernate只能创建2/3。有人能解释一下为什么hibernate不能创建第三个表“商品”吗
1: 组织。冬眠工具架构。spi。CommandAcceptanceException:通过JDBC语句执行DDL“create table GOODES(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”时出错---
<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>
主要:
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
货物:
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;
}
}
我看了你的代码。唯一可疑的是
private String desc;
“DESC”是数据库中的保留关键字。如果仔细查看数据库中发布的语法(查询),它会显示
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
您可以看到“DESC”变为蓝色,表示它是“descending”的保留关键字。尝试将其更改为其他类似“descs”的内容,或者您可以定义列并将其更改为其他类似的内容`
@Column(name="descs")
private String desc;
它会起作用的`
我在尝试将Spring与Hibernate集成时遇到了一些问题。从日志中,表和数据库似乎已经创建,但我找不到数据库文件,在检查sessionFactory时,我得到了nullpointer异常,我无法真正理解发生了什么。 以下是我的配置文件: null 编辑:这是UtenteDaoImpl类,它不做任何事情,因为它继承了BaseDao的东西。
问题内容: 我有以下hibernate.cfg.xml: 我尝试了另一种方言(),但结果却很旧 pom.xml: 当调用以下代码行时: 我看到以下堆栈跟踪: 这个问题的原因是什么? 如何解决? 聚苯乙烯 数据库架构在MySql中不存在! 如果我明确添加数据库shema-一切正常。 从Java应用程序创建架构的方法在哪里? 问题答案: 我通常在使用Spring时使用属性文件自动创建数据库,以下是它的
问题内容: 这是我正在运行的命令: 另外,我正在使用自制Python运行Mac 这是我的setup.py脚本:https : //gist.github.com/cloudformdesign/4791c46fe7cd52eb61cd 我要疯了-我不知道为什么这不起作用。 问题答案: 首先安装软件包: 文档并不太清楚,但是 “ wheel项目为setuptools提供了bdist_wheel命令”
问题内容: 为什么hibernate对这些类使用联接表? 我既不需要联接表也不需要双向关联:( 问题答案: 因为这是它的设计方式,也是JPA规范要求它映射这样的关联的方式。如果要在广告投放表中添加联接列,请使用 这是有据可查的。
问题内容: 我有一个实体: 和一个实体: Hibernate在foo.bar-> bar.id上创建外键约束,但未指定。为什么不?并且有什么方法可以实现? 或者,我可以在数据库中手动添加(并禁用DDL生成), 这是一个好习惯吗? 而且,我是否必须以某种方式修改代码以让Hibernate知道相关记录已被数据库自动删除? 谢谢。 更新 -这是我的JPA / Hibernate / PostgreSQL