简体   繁体   中英

Getting Exception : org.hibernate.exception.sqlgrammarexception

Hi I am implementing hibernate , When I am trying to insert a new record getting the Exception org.hibernate.exception.sqlgrammarexception

My hibernate.cfg.xml file :

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_db</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">system</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <mapping class="com.hib.UserDetails"/>
    </session-factory>
</hibernate-configuration>

If I changed the

<property name="hbm2ddl.auto">update</property>
            <mapping class="com.hib.UserDetails"/>

It worked, But I have to create table manually. What might be the issue?

Look for the piece of code where you are throwing your SQL/HQL query. There you may find some simple yet hard to find mistakes like wrongly spelt commands,etc. That's why sql 'grammar' exception

Check mapping class "UserDeatails".

There is mismatch between manually created table and UserDetails mapping class.

Possibly mismatch Column_Names, Id, Constraints ..etc

check for column names in table and match with column names in @column annotation in bean. Most probably its column mismatch.

Could you post the Stacktrace of the Exception, also it would be of help if you could post the code which is adding the User to Database. It can either be a problem with the Column names of Database table and the declared variables.

<property name="show_sql">true</property>
<property name="format_sql">true</property>

These must be generating the SQL Statement, check if there is an error in the Generated SQL Statement. Posting the SQL could also help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM