Org.hibernate.exception.SQLGrammarException: Failed to insert [com.sample.Person]

I am trying to set up a small working Hibernate sample, which I found here. However, when I run the code, I get a follwing error

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [com.sample.Person] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2345) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2852) at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129) at ..... Caused by: org.postgresql.util.PSQLException: ERROR: relation "person" does not exist Position: 13 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:334) at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94) at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57) ... 23 more 

But I already have a table by the name of the person in the database, and here is my modified hibernate.cfg.xml

  <!-- hibernate dialect --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> <property name="hibernate.connection.url">jdbc:postgresql://localhost/testDB</property> <property name="hibernate.connection.username">postgres</property> <property name="hibernate.connection.password"></property> <property name="hibernate.show.sql" ></property> <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> <!-- Automatic schema creation (begin) === --> <property name="hibernate.hbm2ddl.auto">create</property> <!-- Simple memory-only cache --> <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property> <!-- Enable Hibernate automatic session context management --> <property name="current_session_context_class">thread</property> <!-- ############################################ --> <!-- # mapping files with external dependencies # --> <!-- ############################################ --> <mapping resource="com/sample/Person.hbm.xml" /> </session-factory> 

It would be great if someone could indicate what I am doing wrong, as this is my first attempt at sleep mode. Thanks!

EDIT: Person.hbm.xml

 <class name="com.sample.Person" table="person"> <id name="id" column="ID"> <generator class="native" /> </id> <property name="name"> <column name="NAME" length="16" not-null="true" /> </property> <property name="surname"> <column name="SURNAME" length="16" not-null="true" /> </property> <property name="address"> <column name="ADDRESS" length="16" not-null="true" /> </property> </class> 

EDIT-II: Log File Content (Postgresql.log)

  2012-02-13 09:23:25 IST LOG: database system was shut down at 2012-02-10 18:14:57 IST 2012-02-13 09:23:25 IST FATAL: the database system is starting up 2012-02-13 09:23:33 IST LOG: database system is ready to accept connections 2012-02-13 09:23:38 IST LOG: autovacuum launcher started 2012-02-13 09:46:01 IST ERROR: syntax error at or near "auto_increment" at character 41 2012-02-13 09:46:01 IST STATEMENT: create table person (ID bigint not null auto_increment, NAME varchar(16) not null, SURNAME varchar(16) not null, ADDRESS varchar(16) not null, primary key (ID)) type=InnoDB 2012-02-13 09:46:01 IST ERROR: relation "person" does not exist at character 13 2012-02-13 09:46:01 IST STATEMENT: insert into person (NAME, SURNAME, ADDRESS) values ($1, $2, $3) RETURNING * 2012-02-13 09:46:01 IST LOG: could not receive data from client: No connection could be made because the target machine actively refused it. 2012-02-13 09:46:01 IST LOG: unexpected EOF on client connection 2012-02-13 09:48:15 IST ERROR: syntax error at or near "auto_increment" at character 41 2012-02-13 09:48:15 IST STATEMENT: create table person (ID bigint not null auto_increment, NAME varchar(16) not null, SURNAME varchar(16) not null, ADDRESS varchar(16) not null, primary key (ID)) type=InnoDB 2012-02-13 09:48:15 IST ERROR: relation "person" does not exist at character 13 2012-02-13 09:48:15 IST STATEMENT: insert into person (NAME, SURNAME, ADDRESS) values ($1, $2, $3) RETURNING * 2012-02-13 09:48:15 IST LOG: could not receive data from client: No connection could be made because the target machine actively refused it. 2012-02-13 09:48:15 IST LOG: unexpected EOF on client connection 

UPDATE : I noticed something strange, I create a relation in the database, and then run this piece of code, only to see that the table is deleted, because it just disappears when I run this code; any idea why this is happening?

+6
source share
9 answers

I resolved the error by changing the following property in the hibernate.cfg.xml file

  <property name="hibernate.hbm2ddl.auto">validate</property> 

Previously, the table was deleted every time I ran the program, and now it does not work, since sleep mode only checks the scheme and does not affect its changes.

+8
source

According to the exception, Hibernate wants to write "person" to the table, but in your hbm.xml you determine that the "Person" table exists, are you sure that the correct table exists in your database schema?

+4
source

Note:

  • You do not need to specify the table name in Person.hbm.xml (........) when you create a table with the same class name. Also applicable to fields.

  • When creating the "person" table in your respective database, make sure that any FILEDS names you specify in Person.hbm.xml must match the COLUMNS ELSE column names, you will get the error above.

+2
source

The problem in my case was that the database name was incorrect.
I solved the problem by entering the correct database name in the box below

 <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myDatabase</property> 
+1
source

It seems you are connecting to the wrong database. R u sure " jdbc:postgresql://localhost/testDB" will bind you to the actual data source?

As a rule, they look like "jdbc://hostname/databasename". Look into the Postgresql log file.

0
source

I resolved the error by changing the following property in the hibernate.cfg.xml file

<property name="hibernate.hbm2ddl.auto">validate</property>

Previously, the table was deleted every time I ran the program, and now this is not so, since sleep mode only checks the scheme and does not affect its changes.

As far as I know, you can also change the update check rule, for example:

 <property name="hibernate.hbm2ddl.auto">update</property> 
0
source

You can try to put the correct database name in the connection url in the configuration file. Since I had the same error when starting a POJO class file, and it was solved by this.

0
source

I handled these errors by changing the database hardset. Generic database encoding cp1252 and i convert to utf-8

0
source

What do we mean by org.hibernate.exception.SQLGrammarException ?

An implementation of JDBCException indicating that SQL sent to the database server was invalid (syntax error, invalid object references, etc.).

and in my opinion there is some Grammar error inside your hibernate.cfg.xml configuration file,

this happens when you write the wrong name for the defination property of the schema inside, as shown below:

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

which should be as follows:

 <property name="hibernate.hbm2ddl.auto">create</property> 
0
source

Source: https://habr.com/ru/post/908139/


All Articles