Reputation: 87
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.
Upvotes: 0
Views: 1497
Reputation: 87
I solved, the problem was a property of my hibernate.cfg.xml, from this:
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
to this:
<property name="dialect">org.hibernate.dialect.MariaDBDialect</property>
In the main I tried not to save the product and it gave me another sql error which helped me to find what was going wrong.
Upvotes: 1
Reputation: 11
in your hibernate xml when you see the line
<property name="connection.url">jdbc:mysql://localhost:3306/cube</property>
it means you have a database called "cube" on some MySQL server installed on your computer. Are you doing this?
If you have the database running somewhere on your computer then you should check if you created the table called "products"
the error is when you try to access table cube.products
So in your Product class,
when you use the annotation
@Table(name="products")
that implies that you already have a table that exist.
So you should try to
Upvotes: 0