0

I am facing this error for quite a while now. I have managed to find a work-around by mapping the entity in my HibernateUtil class. However, I would like to achieve this using hibernate.cfg.xml.

Error that I am getting:

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1441)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:491)
at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3201)
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2411)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:467)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:146)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:220)
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)
at Main.main(Main.java:14)

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mydatabase.personentity' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2490)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:205)
... 18 more

Hibernate Config:

<?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>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydatabase</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<property name="connection.pool_size">1</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- List of mapped classes -->
<mapping class="PersonEntity"/>

</session-factory>
</hibernate-configuration>

Entity:

@Entity
public class PersonEntity
{
private int id;
private String name;

@Id
@Column(name = "ID")
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
@Column(name = "Name")
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
}

Hibernate Util:

public class HibernateUtil
{
private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
    Configuration configuration = new Configuration();
    configuration.configure();        

    StandardServiceRegistry standardRegistry = new 
    StandardServiceRegistryBuilder()
            .configure()
            .build();

    return configuration.buildSessionFactory(standardRegistry);
}
catch(Exception e) {
    throw new ExceptionInInitializerError(e);
}
}

public static Session getSession()
{
return sessionFactory.openSession();
}

public static void close()
{
sessionFactory.close();
}
}

Main class which is meant to save some hardcoded values:

public class Main {
public static void main(String[] args) {
    PersonEntity personEntity = new PersonEntity();

    personEntity.setId(1);
    personEntity.setName("Cris");

    Session session = HibernateUtil.getSession();

    session.beginTransaction();
    session.save(personEntity);
    session.getTransaction().commit();

}

}

I would be grateful if someone could help, in my opinion the problem lays in hibernate.cfg.xml, but that's just my guess. Thanks in advance

Kris22
  • 15
  • 1
  • 5

2 Answers2

0

If the table exists in the database, for example naming person, then you should annotate the entity with @Table:

@Entity
@Table(name = "person")
public class PersonEntity

If you want hibernate to create the table for you, you should change hbm2ddl.auto to create at first, then change it back to update after the table has been created.

<property name="hbm2ddl.auto">create</property>

BTW, the mapping class in hibernate.cfg.xml should be full package name.

Dave Pateral
  • 1,415
  • 1
  • 14
  • 21
0

Try changing <property name="hbm2ddl.auto"> to <property name="hibernate.hbm2ddl.auto"> (document says so)

You should change your Sql Dialect, too. For MySQL 5.x, you should use org.hibernate.dialect.MySQL5InnoDBDialect if you are using InnoDB tables or org.hibernate.dialect.MySQL5Dialect if you're not.

Refer to Session Configuration document.

Cà phê đen
  • 1,883
  • 2
  • 21
  • 20
  • 1
    I had to change Dialect to org.hibernate.dialect.MySQL5Dialect Thank you very much and everyone else for the help. – Kris22 Jul 06 '17 at 08:55
  • @Kris22 you should accept the answer if it helps to solve your problem, which is very useful for later-coming users. :) Please refer this: [Accepting Answers: How does it work?](https://meta.stackexchange.com/a/5235) – Cà phê đen Jul 06 '17 at 09:17
  • Done it :) Thanks. – Kris22 Jul 06 '17 at 10:11