Java Hibernate不创建表
我想使用hibernate从java对象创建表,但它给了我以下错误: org.hibernate.exception.sqlgrammareexception:无法在database.Starter.main(Starter.java:51)处执行语句,原因是:java.sql.SQLSyntaxErrorException:表“cube.products”不存在 它不应该给我这个错误,因为表不应该在那里。下面是我的hibernate.cfg.xml:Java Hibernate不创建表,java,hibernate,Java,Hibernate,我想使用hibernate从java对象创建表,但它给了我以下错误: org.hibernate.exception.sqlgrammareexception:无法在database.Starter.main(Starter.java:51)处执行语句,原因是:java.sql.SQLSyntaxErrorException:表“cube.products”不存在 它不应该给我这个错误,因为表不应该在那里。下面是我的hibernate.cfg.xml: <!DOCTYPE hibe
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/cube</property>
<property name="connection.username">user</property>
<property name="connection.password">pass</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="database.Product" />
</session-factory>
</hibernate-configuration>
下面是名为Starter的主类:
package database;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import java.util.List;
public class Starter {
public static void main(String args[]) {
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure("hibernate.cfg.xml")
.build();
try {
SessionFactory factory = new MetadataSources(registry)
.buildMetadata().buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
Product product = new Product();
product.setName("iPhone 7 Plus");
product.setDescription("A good smartphone");
product.setPrice(1299.89f);
product.setEnabled(true);
session.save(product);
transaction.commit();
session.close();
factory.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
StandardServiceRegistryBuilder.destroy(registry);
}
}
}
我希望有人能帮助我,因为我真的不知道该多尝试些什么。我问这个问题是为了分散注意力。在您的HibernateXML中,当您看到行时
<property name="connection.url">jdbc:mysql://localhost:3306/cube</property>
jdbc:mysql://localhost:3306/cube
这意味着您的计算机上安装了一个名为“多维数据集”的数据库。你在这么做吗
如果您的计算机上运行着数据库,那么您应该检查是否创建了名为“products”的表
错误出现在尝试访问表cube.products时
所以在你的产品课上
使用注释时
@Table(name=“products”)
这意味着您已经有了一个表。
所以你应该试着
- 首先运行代码来创建表
- 第二步使用hibernate CRUD操作运行项目
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
org.hibernate.dialogue.mysqldialogue
为此:
<property name="dialect">org.hibernate.dialect.MariaDBDialect</property>
org.hibernate.dial.MariaDBDialect
总的来说,我试图不保存产品,但它给了我另一个sql错误,帮助我找到了问题所在。是的,我的计算机上运行了名为cube的数据库。在启动代码之前的“products”表不应该存在,因为我想用hibernate创建它,但不知怎的,它给了我这个错误。我将代码与网络上不同教程中出现的代码进行了比较,结果显示它们很好。是的,在产品类中,当您使用注释@Table(name=“products”)时,这意味着您已经有了一个表。因此,您应该首先运行代码来创建表,然后使用hibernate运行项目。该行应该根据映射的实体类创建表。查看本文,其中包含我正在测试的代码:
<property name="dialect">org.hibernate.dialect.MariaDBDialect</property>