我一直在尝试让我的 Java 应用程序与 Hibernate 连接到 localhost:3306 中的 MySQL 数据库并创建一个表,但它没有发生。它给出了错误。但是,当我预先按给定名称创建表然后运行应用程序时,相同的代码会起作用,它会相应地在表中插入所有数据。
在 hibernate.cfg.xml 文件中我尝试设置我的
“update”既可以更新也可以创建。当数据库中没有名为“Alien”的表时,这些都不起作用。
根据我的理解,当此选项设置为“更新”时,它会创建一个表并插入数据(当表不存在时,它只会在表已经存在时插入数据。
请帮我解决这个问题。
这是我的代码:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ksk</groupId>
<artifactId>Hibernate Project</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
</dependencies>
</project>
hibernate.cfg.xml
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name = "hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- Assume test is the database name -->
<property name = "hibernate.connection.url">
jdbc:mysql://localhost:3306/myHibernate
</property>
<property name = "hibernate.connection.username">
ksk235
</property>
<property name = "hibernate.connection.password">
password
</property>
<property name = "hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
外星人.java
package com.ksk;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Alien {
@Id
private int aid;
private String aName;
private String color;
public Alien(){};
public int getAid() {
return aid;
}
public String getaName() {
return aName;
}
public String getColor() {
return color;
}
public void setAid(int aid) {
this.aid = aid;
}
public void setaName(String aName) {
this.aName = aName;
}
public void setColor(String color) {
this.color = color;
}
}
应用程序.java
package com.ksk;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class App {
public static void main(String[] args) {
Alien kaushik =new Alien();
kaushik.setAid(101);
kaushik.setaName("Kaushik");
kaushik.setColor("Green");
Configuration config = new
Configuration().configure().addAnnotatedClass(Alien.class);
SessionFactory sf = config.buildSessionFactory();
Session session =sf.openSession();
Transaction tx = session.beginTransaction();
session.save(kaushik);
tx.commit();
}
}
我收到的错误可以在此链接中查看: https://1drv.ms/w/s!AgnhrqMHRiyJjfll32wEkDQRSOQ4Yw
最佳答案
(1) 在文件 hibernate.cfg.xml
中,
<property name="hbm2ddl.auto">update</property>
应该是
<property name="hibernate.hbm2ddl.auto">create</property>
(2) 例如,您还需要指出文件 DDL 用于自动化任务
<property name="hibernate.hbm2ddl.import_files">classpath:/WEB-INF/classes/myfolder/myfile.sql</property>
报价
File order matters, the statements of a give file are executed before the statements of the following files. These statements are only executed if the schema is created ie if hibernate.hbm2ddl.auto is set to create or create-drop.
例如/人类.sql,/狗.sql
参见引用文档:https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch03.html#configuration-misc-properties
关于java - Hibernate 表不会自动创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48887781/