I want to create table from java object using hibernate but it gives me this error:
org.hibernate.exception.SQLGrammarException: could not execute statement <27 internal call> at database.Starter.main(Starter.java:51) <5 internal call> Caused by: java.sql.SQLSyntaxErrorException: Table 'cube.products' doesn't exist
It should not give me this error because the table should not be supposed to be there. Below you find my hibernate.cfg.xml:
<!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> Below you find my Product class (without getter and setter avoid inserting to much code):
package database; import javax.persistence.*; @Entity @Table(name = "products") public class Product { @Id @Column(name = "product_id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(name = "name", length = 128, nullable = true, unique = false) private String name; @Column(name = "price", precision = 10, scale = 2) private float price; @Column(name = "enabled", columnDefinition = "tinyint default 1") private boolean enabled; @Column(length = 512, nullable = true) private String description; And below you find the main class called 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); } } } I hope someone can help me cause I really don't know what to try more. I asked this question as a disperate move.
https://stackoverflow.com/questions/65555810/hibernate-does-not-create-table January 04, 2021 at 07:01AM
没有评论:
发表评论